From 8a3151728875f0fc542f0fe7e32e728fe28cd657 Mon Sep 17 00:00:00 2001 From: Toshi MARUYAMA Date: Mon, 4 Apr 2011 11:53:29 +0000 Subject: [PATCH] add bulk edit and bulk update actions for time entries (#7996). get bulk edit form action working by mapping permissions. Contributed by Adam Soltys. git-svn-id: svn+ssh://rubyforge.org/var/svn/redmine/trunk@5313 e93f8b46-1217-0410-a6f0-8f06a7374b81 --- app/controllers/timelog_controller.rb | 49 +++++++++++++++++++++++++++ app/models/project.rb | 6 ++++ config/routes.rb | 7 ++++ lib/redmine.rb | 6 ++-- 4 files changed, 65 insertions(+), 3 deletions(-) diff --git a/app/controllers/timelog_controller.rb b/app/controllers/timelog_controller.rb index 604a8f244..6d78c514f 100644 --- a/app/controllers/timelog_controller.rb +++ b/app/controllers/timelog_controller.rb @@ -19,6 +19,7 @@ class TimelogController < ApplicationController menu_item :issues before_filter :find_project, :only => [:new, :create] before_filter :find_time_entry, :only => [:show, :edit, :update, :destroy] + before_filter :find_time_entries, :only => [:bulk_edit, :bulk_update] before_filter :authorize, :except => [:index] before_filter :find_optional_project, :only => [:index] accept_key_auth :index, :show, :create, :update, :destroy @@ -160,6 +161,28 @@ class TimelogController < ApplicationController end end + def bulk_edit + @available_activities = TimeEntryActivity.shared.active + @custom_fields = TimeEntry.first.available_custom_fields + end + + def bulk_update + attributes = parse_params_for_bulk_time_entry_attributes(params) + + unsaved_time_entry_ids = [] + @time_entries.each do |time_entry| + time_entry.reload + time_entry.attributes = attributes + call_hook(:controller_time_entries_bulk_edit_before_save, { :params => params, :time_entry => time_entry }) + unless time_entry.save + # Keep unsaved time_entry ids to display them in flash error + unsaved_time_entry_ids << time_entry.id + end + end + set_flash_from_bulk_time_entry_save(@time_entries, unsaved_time_entry_ids) + redirect_back_or_default({:controller => 'timelog', :action => 'index', :project_id => @project}) + end + verify :method => :delete, :only => :destroy, :render => {:nothing => true, :status => :method_not_allowed } def destroy if @time_entry.destroy && @time_entry.destroyed? @@ -195,6 +218,26 @@ private render_404 end + def find_time_entries + @time_entries = TimeEntry.find_all_by_id(params[:id] || params[:ids]) + raise ActiveRecord::RecordNotFound if @time_entries.empty? + @projects = @time_entries.collect(&:project).compact.uniq + @project = @projects.first if @projects.size == 1 + rescue ActiveRecord::RecordNotFound + render_404 + end + + def set_flash_from_bulk_time_entry_save(time_entries, unsaved_time_entry_ids) + if unsaved_time_entry_ids.empty? + flash[:notice] = l(:notice_successful_update) unless time_entries.empty? + else + flash[:error] = l(:notice_failed_to_save_time_entries, + :count => unsaved_time_entry_ids.size, + :total => time_entries.size, + :ids => '#' + unsaved_time_entry_ids.join(', #')) + end + end + def find_project if (issue_id = (params[:issue_id] || params[:time_entry] && params[:time_entry][:issue_id])).present? @issue = Issue.find(issue_id) @@ -265,4 +308,10 @@ private @to ||= (TimeEntry.latest_date_for_project(@project) || Date.today) end + def parse_params_for_bulk_time_entry_attributes(params) + attributes = (params[:time_entry] || {}).reject {|k,v| v.blank?} + attributes.keys.each {|k| attributes[k] = '' if attributes[k] == 'none'} + attributes[:custom_field_values].reject! {|k,v| v.blank?} if attributes[:custom_field_values] + attributes + end end diff --git a/app/models/project.rb b/app/models/project.rb index 780a32ed8..58839a6fe 100644 --- a/app/models/project.rb +++ b/app/models/project.rb @@ -431,6 +431,12 @@ class Project < ActiveRecord::Base def all_issue_custom_fields @all_issue_custom_fields ||= (IssueCustomField.for_all + issue_custom_fields).uniq.sort end + + # Returns an array of all custom fields enabled for project time entries + # (explictly associated custom fields and custom fields enabled for all projects) + def all_time_entry_custom_fields + @all_time_entry_custom_fields ||= (TimeEntryCustomField.for_all + time_entry_custom_fields).uniq.sort + end def project self diff --git a/config/routes.rb b/config/routes.rb index 7daac2a1a..a019881a8 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -23,6 +23,13 @@ ActionController::Routing::Routes.draw do |map| time_report.connect 'time_entries/report.:format' end + map.bulk_edit_time_entry 'time_entries/bulk_edit', + :controller => 'timelog', :action => 'bulk_edit', :conditions => { :method => :get } + map.bulk_update_time_entry 'time_entries/bulk_edit', + :controller => 'timelog', :action => 'bulk_update', :conditions => { :method => :post } + map.time_entries_context_menu '/time_entries/context_menu', + :controller => 'context_menus', :action => 'time_entries' + # TODO: wasteful since this is also nested under issues, projects, and projects/issues map.resources :time_entries, :controller => 'timelog' map.connect 'projects/:id/wiki', :controller => 'wikis', :action => 'edit', :conditions => {:method => :post} diff --git a/lib/redmine.rb b/lib/redmine.rb index 631f10946..5ecf2eabf 100644 --- a/lib/redmine.rb +++ b/lib/redmine.rb @@ -86,10 +86,10 @@ Redmine::AccessControl.map do |map| end map.project_module :time_tracking do |map| - map.permission :log_time, {:timelog => [:new, :create, :edit, :update]}, :require => :loggedin + map.permission :log_time, {:timelog => [:new, :create, :edit, :update, :bulk_edit, :bulk_update]}, :require => :loggedin map.permission :view_time_entries, :timelog => [:index, :show], :time_entry_reports => [:report] - map.permission :edit_time_entries, {:timelog => [:new, :create, :edit, :update, :destroy]}, :require => :member - map.permission :edit_own_time_entries, {:timelog => [:new, :create, :edit, :update, :destroy]}, :require => :loggedin + map.permission :edit_time_entries, {:timelog => [:new, :create, :edit, :update, :destroy, :bulk_edit, :bulk_update]}, :require => :member + map.permission :edit_own_time_entries, {:timelog => [:new, :create, :edit, :update, :destroy,:bulk_edit, :bulk_update]}, :require => :loggedin map.permission :manage_project_activities, {:project_enumerations => [:update, :destroy]}, :require => :member end