##// END OF EJS Templates
Log errors when bulk edit/copy fails (#13440)....
Jean-Philippe Lang -
r11381:1d48ab201b36
parent child
Show More
@@ -1,441 +1,442
1 # Redmine - project management software
1 # Redmine - project management software
2 # Copyright (C) 2006-2013 Jean-Philippe Lang
2 # Copyright (C) 2006-2013 Jean-Philippe Lang
3 #
3 #
4 # This program is free software; you can redistribute it and/or
4 # This program is free software; you can redistribute it and/or
5 # modify it under the terms of the GNU General Public License
5 # modify it under the terms of the GNU General Public License
6 # as published by the Free Software Foundation; either version 2
6 # as published by the Free Software Foundation; either version 2
7 # of the License, or (at your option) any later version.
7 # of the License, or (at your option) any later version.
8 #
8 #
9 # This program is distributed in the hope that it will be useful,
9 # This program is distributed in the hope that it will be useful,
10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 # GNU General Public License for more details.
12 # GNU General Public License for more details.
13 #
13 #
14 # You should have received a copy of the GNU General Public License
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
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.
16 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
17
17
18 class IssuesController < ApplicationController
18 class IssuesController < ApplicationController
19 menu_item :new_issue, :only => [:new, :create]
19 menu_item :new_issue, :only => [:new, :create]
20 default_search_scope :issues
20 default_search_scope :issues
21
21
22 before_filter :find_issue, :only => [:show, :edit, :update]
22 before_filter :find_issue, :only => [:show, :edit, :update]
23 before_filter :find_issues, :only => [:bulk_edit, :bulk_update, :destroy]
23 before_filter :find_issues, :only => [:bulk_edit, :bulk_update, :destroy]
24 before_filter :find_project, :only => [:new, :create, :update_form]
24 before_filter :find_project, :only => [:new, :create, :update_form]
25 before_filter :authorize, :except => [:index]
25 before_filter :authorize, :except => [:index]
26 before_filter :find_optional_project, :only => [:index]
26 before_filter :find_optional_project, :only => [:index]
27 before_filter :check_for_default_issue_status, :only => [:new, :create]
27 before_filter :check_for_default_issue_status, :only => [:new, :create]
28 before_filter :build_new_issue_from_params, :only => [:new, :create, :update_form]
28 before_filter :build_new_issue_from_params, :only => [:new, :create, :update_form]
29 accept_rss_auth :index, :show
29 accept_rss_auth :index, :show
30 accept_api_auth :index, :show, :create, :update, :destroy
30 accept_api_auth :index, :show, :create, :update, :destroy
31
31
32 rescue_from Query::StatementInvalid, :with => :query_statement_invalid
32 rescue_from Query::StatementInvalid, :with => :query_statement_invalid
33
33
34 helper :journals
34 helper :journals
35 helper :projects
35 helper :projects
36 include ProjectsHelper
36 include ProjectsHelper
37 helper :custom_fields
37 helper :custom_fields
38 include CustomFieldsHelper
38 include CustomFieldsHelper
39 helper :issue_relations
39 helper :issue_relations
40 include IssueRelationsHelper
40 include IssueRelationsHelper
41 helper :watchers
41 helper :watchers
42 include WatchersHelper
42 include WatchersHelper
43 helper :attachments
43 helper :attachments
44 include AttachmentsHelper
44 include AttachmentsHelper
45 helper :queries
45 helper :queries
46 include QueriesHelper
46 include QueriesHelper
47 helper :repositories
47 helper :repositories
48 include RepositoriesHelper
48 include RepositoriesHelper
49 helper :sort
49 helper :sort
50 include SortHelper
50 include SortHelper
51 include IssuesHelper
51 include IssuesHelper
52 helper :timelog
52 helper :timelog
53 include Redmine::Export::PDF
53 include Redmine::Export::PDF
54
54
55 def index
55 def index
56 retrieve_query
56 retrieve_query
57 sort_init(@query.sort_criteria.empty? ? [['id', 'desc']] : @query.sort_criteria)
57 sort_init(@query.sort_criteria.empty? ? [['id', 'desc']] : @query.sort_criteria)
58 sort_update(@query.sortable_columns)
58 sort_update(@query.sortable_columns)
59 @query.sort_criteria = sort_criteria.to_a
59 @query.sort_criteria = sort_criteria.to_a
60
60
61 if @query.valid?
61 if @query.valid?
62 case params[:format]
62 case params[:format]
63 when 'csv', 'pdf'
63 when 'csv', 'pdf'
64 @limit = Setting.issues_export_limit.to_i
64 @limit = Setting.issues_export_limit.to_i
65 when 'atom'
65 when 'atom'
66 @limit = Setting.feeds_limit.to_i
66 @limit = Setting.feeds_limit.to_i
67 when 'xml', 'json'
67 when 'xml', 'json'
68 @offset, @limit = api_offset_and_limit
68 @offset, @limit = api_offset_and_limit
69 else
69 else
70 @limit = per_page_option
70 @limit = per_page_option
71 end
71 end
72
72
73 @issue_count = @query.issue_count
73 @issue_count = @query.issue_count
74 @issue_pages = Paginator.new @issue_count, @limit, params['page']
74 @issue_pages = Paginator.new @issue_count, @limit, params['page']
75 @offset ||= @issue_pages.offset
75 @offset ||= @issue_pages.offset
76 @issues = @query.issues(:include => [:assigned_to, :tracker, :priority, :category, :fixed_version],
76 @issues = @query.issues(:include => [:assigned_to, :tracker, :priority, :category, :fixed_version],
77 :order => sort_clause,
77 :order => sort_clause,
78 :offset => @offset,
78 :offset => @offset,
79 :limit => @limit)
79 :limit => @limit)
80 @issue_count_by_group = @query.issue_count_by_group
80 @issue_count_by_group = @query.issue_count_by_group
81
81
82 respond_to do |format|
82 respond_to do |format|
83 format.html { render :template => 'issues/index', :layout => !request.xhr? }
83 format.html { render :template => 'issues/index', :layout => !request.xhr? }
84 format.api {
84 format.api {
85 Issue.load_visible_relations(@issues) if include_in_api_response?('relations')
85 Issue.load_visible_relations(@issues) if include_in_api_response?('relations')
86 }
86 }
87 format.atom { render_feed(@issues, :title => "#{@project || Setting.app_title}: #{l(:label_issue_plural)}") }
87 format.atom { render_feed(@issues, :title => "#{@project || Setting.app_title}: #{l(:label_issue_plural)}") }
88 format.csv { send_data(query_to_csv(@issues, @query, params), :type => 'text/csv; header=present', :filename => 'issues.csv') }
88 format.csv { send_data(query_to_csv(@issues, @query, params), :type => 'text/csv; header=present', :filename => 'issues.csv') }
89 format.pdf { send_data(issues_to_pdf(@issues, @project, @query), :type => 'application/pdf', :filename => 'issues.pdf') }
89 format.pdf { send_data(issues_to_pdf(@issues, @project, @query), :type => 'application/pdf', :filename => 'issues.pdf') }
90 end
90 end
91 else
91 else
92 respond_to do |format|
92 respond_to do |format|
93 format.html { render(:template => 'issues/index', :layout => !request.xhr?) }
93 format.html { render(:template => 'issues/index', :layout => !request.xhr?) }
94 format.any(:atom, :csv, :pdf) { render(:nothing => true) }
94 format.any(:atom, :csv, :pdf) { render(:nothing => true) }
95 format.api { render_validation_errors(@query) }
95 format.api { render_validation_errors(@query) }
96 end
96 end
97 end
97 end
98 rescue ActiveRecord::RecordNotFound
98 rescue ActiveRecord::RecordNotFound
99 render_404
99 render_404
100 end
100 end
101
101
102 def show
102 def show
103 @journals = @issue.journals.includes(:user, :details).reorder("#{Journal.table_name}.id ASC").all
103 @journals = @issue.journals.includes(:user, :details).reorder("#{Journal.table_name}.id ASC").all
104 @journals.each_with_index {|j,i| j.indice = i+1}
104 @journals.each_with_index {|j,i| j.indice = i+1}
105 @journals.reject!(&:private_notes?) unless User.current.allowed_to?(:view_private_notes, @issue.project)
105 @journals.reject!(&:private_notes?) unless User.current.allowed_to?(:view_private_notes, @issue.project)
106 @journals.reverse! if User.current.wants_comments_in_reverse_order?
106 @journals.reverse! if User.current.wants_comments_in_reverse_order?
107
107
108 @changesets = @issue.changesets.visible.all
108 @changesets = @issue.changesets.visible.all
109 @changesets.reverse! if User.current.wants_comments_in_reverse_order?
109 @changesets.reverse! if User.current.wants_comments_in_reverse_order?
110
110
111 @relations = @issue.relations.select {|r| r.other_issue(@issue) && r.other_issue(@issue).visible? }
111 @relations = @issue.relations.select {|r| r.other_issue(@issue) && r.other_issue(@issue).visible? }
112 @allowed_statuses = @issue.new_statuses_allowed_to(User.current)
112 @allowed_statuses = @issue.new_statuses_allowed_to(User.current)
113 @edit_allowed = User.current.allowed_to?(:edit_issues, @project)
113 @edit_allowed = User.current.allowed_to?(:edit_issues, @project)
114 @priorities = IssuePriority.active
114 @priorities = IssuePriority.active
115 @time_entry = TimeEntry.new(:issue => @issue, :project => @issue.project)
115 @time_entry = TimeEntry.new(:issue => @issue, :project => @issue.project)
116 @relation = IssueRelation.new
116 @relation = IssueRelation.new
117
117
118 respond_to do |format|
118 respond_to do |format|
119 format.html {
119 format.html {
120 retrieve_previous_and_next_issue_ids
120 retrieve_previous_and_next_issue_ids
121 render :template => 'issues/show'
121 render :template => 'issues/show'
122 }
122 }
123 format.api
123 format.api
124 format.atom { render :template => 'journals/index', :layout => false, :content_type => 'application/atom+xml' }
124 format.atom { render :template => 'journals/index', :layout => false, :content_type => 'application/atom+xml' }
125 format.pdf {
125 format.pdf {
126 pdf = issue_to_pdf(@issue, :journals => @journals)
126 pdf = issue_to_pdf(@issue, :journals => @journals)
127 send_data(pdf, :type => 'application/pdf', :filename => "#{@project.identifier}-#{@issue.id}.pdf")
127 send_data(pdf, :type => 'application/pdf', :filename => "#{@project.identifier}-#{@issue.id}.pdf")
128 }
128 }
129 end
129 end
130 end
130 end
131
131
132 # Add a new issue
132 # Add a new issue
133 # The new issue will be created from an existing one if copy_from parameter is given
133 # The new issue will be created from an existing one if copy_from parameter is given
134 def new
134 def new
135 respond_to do |format|
135 respond_to do |format|
136 format.html { render :action => 'new', :layout => !request.xhr? }
136 format.html { render :action => 'new', :layout => !request.xhr? }
137 end
137 end
138 end
138 end
139
139
140 def create
140 def create
141 call_hook(:controller_issues_new_before_save, { :params => params, :issue => @issue })
141 call_hook(:controller_issues_new_before_save, { :params => params, :issue => @issue })
142 @issue.save_attachments(params[:attachments] || (params[:issue] && params[:issue][:uploads]))
142 @issue.save_attachments(params[:attachments] || (params[:issue] && params[:issue][:uploads]))
143 if @issue.save
143 if @issue.save
144 call_hook(:controller_issues_new_after_save, { :params => params, :issue => @issue})
144 call_hook(:controller_issues_new_after_save, { :params => params, :issue => @issue})
145 respond_to do |format|
145 respond_to do |format|
146 format.html {
146 format.html {
147 render_attachment_warning_if_needed(@issue)
147 render_attachment_warning_if_needed(@issue)
148 flash[:notice] = l(:notice_issue_successful_create, :id => view_context.link_to("##{@issue.id}", issue_path(@issue), :title => @issue.subject))
148 flash[:notice] = l(:notice_issue_successful_create, :id => view_context.link_to("##{@issue.id}", issue_path(@issue), :title => @issue.subject))
149 if params[:continue]
149 if params[:continue]
150 attrs = {:tracker_id => @issue.tracker, :parent_issue_id => @issue.parent_issue_id}.reject {|k,v| v.nil?}
150 attrs = {:tracker_id => @issue.tracker, :parent_issue_id => @issue.parent_issue_id}.reject {|k,v| v.nil?}
151 redirect_to new_project_issue_path(@issue.project, :issue => attrs)
151 redirect_to new_project_issue_path(@issue.project, :issue => attrs)
152 else
152 else
153 redirect_to issue_path(@issue)
153 redirect_to issue_path(@issue)
154 end
154 end
155 }
155 }
156 format.api { render :action => 'show', :status => :created, :location => issue_url(@issue) }
156 format.api { render :action => 'show', :status => :created, :location => issue_url(@issue) }
157 end
157 end
158 return
158 return
159 else
159 else
160 respond_to do |format|
160 respond_to do |format|
161 format.html { render :action => 'new' }
161 format.html { render :action => 'new' }
162 format.api { render_validation_errors(@issue) }
162 format.api { render_validation_errors(@issue) }
163 end
163 end
164 end
164 end
165 end
165 end
166
166
167 def edit
167 def edit
168 return unless update_issue_from_params
168 return unless update_issue_from_params
169
169
170 respond_to do |format|
170 respond_to do |format|
171 format.html { }
171 format.html { }
172 format.xml { }
172 format.xml { }
173 end
173 end
174 end
174 end
175
175
176 def update
176 def update
177 return unless update_issue_from_params
177 return unless update_issue_from_params
178 @issue.save_attachments(params[:attachments] || (params[:issue] && params[:issue][:uploads]))
178 @issue.save_attachments(params[:attachments] || (params[:issue] && params[:issue][:uploads]))
179 saved = false
179 saved = false
180 begin
180 begin
181 saved = @issue.save_issue_with_child_records(params, @time_entry)
181 saved = @issue.save_issue_with_child_records(params, @time_entry)
182 rescue ActiveRecord::StaleObjectError
182 rescue ActiveRecord::StaleObjectError
183 @conflict = true
183 @conflict = true
184 if params[:last_journal_id]
184 if params[:last_journal_id]
185 @conflict_journals = @issue.journals_after(params[:last_journal_id]).all
185 @conflict_journals = @issue.journals_after(params[:last_journal_id]).all
186 @conflict_journals.reject!(&:private_notes?) unless User.current.allowed_to?(:view_private_notes, @issue.project)
186 @conflict_journals.reject!(&:private_notes?) unless User.current.allowed_to?(:view_private_notes, @issue.project)
187 end
187 end
188 end
188 end
189
189
190 if saved
190 if saved
191 render_attachment_warning_if_needed(@issue)
191 render_attachment_warning_if_needed(@issue)
192 flash[:notice] = l(:notice_successful_update) unless @issue.current_journal.new_record?
192 flash[:notice] = l(:notice_successful_update) unless @issue.current_journal.new_record?
193
193
194 respond_to do |format|
194 respond_to do |format|
195 format.html { redirect_back_or_default issue_path(@issue) }
195 format.html { redirect_back_or_default issue_path(@issue) }
196 format.api { render_api_ok }
196 format.api { render_api_ok }
197 end
197 end
198 else
198 else
199 respond_to do |format|
199 respond_to do |format|
200 format.html { render :action => 'edit' }
200 format.html { render :action => 'edit' }
201 format.api { render_validation_errors(@issue) }
201 format.api { render_validation_errors(@issue) }
202 end
202 end
203 end
203 end
204 end
204 end
205
205
206 # Updates the issue form when changing the project, status or tracker
206 # Updates the issue form when changing the project, status or tracker
207 # on issue creation/update
207 # on issue creation/update
208 def update_form
208 def update_form
209 end
209 end
210
210
211 # Bulk edit/copy a set of issues
211 # Bulk edit/copy a set of issues
212 def bulk_edit
212 def bulk_edit
213 @issues.sort!
213 @issues.sort!
214 @copy = params[:copy].present?
214 @copy = params[:copy].present?
215 @notes = params[:notes]
215 @notes = params[:notes]
216
216
217 if User.current.allowed_to?(:move_issues, @projects)
217 if User.current.allowed_to?(:move_issues, @projects)
218 @allowed_projects = Issue.allowed_target_projects_on_move
218 @allowed_projects = Issue.allowed_target_projects_on_move
219 if params[:issue]
219 if params[:issue]
220 @target_project = @allowed_projects.detect {|p| p.id.to_s == params[:issue][:project_id].to_s}
220 @target_project = @allowed_projects.detect {|p| p.id.to_s == params[:issue][:project_id].to_s}
221 if @target_project
221 if @target_project
222 target_projects = [@target_project]
222 target_projects = [@target_project]
223 end
223 end
224 end
224 end
225 end
225 end
226 target_projects ||= @projects
226 target_projects ||= @projects
227
227
228 if @copy
228 if @copy
229 @available_statuses = [IssueStatus.default]
229 @available_statuses = [IssueStatus.default]
230 else
230 else
231 @available_statuses = @issues.map(&:new_statuses_allowed_to).reduce(:&)
231 @available_statuses = @issues.map(&:new_statuses_allowed_to).reduce(:&)
232 end
232 end
233 @custom_fields = target_projects.map{|p|p.all_issue_custom_fields}.reduce(:&)
233 @custom_fields = target_projects.map{|p|p.all_issue_custom_fields}.reduce(:&)
234 @assignables = target_projects.map(&:assignable_users).reduce(:&)
234 @assignables = target_projects.map(&:assignable_users).reduce(:&)
235 @trackers = target_projects.map(&:trackers).reduce(:&)
235 @trackers = target_projects.map(&:trackers).reduce(:&)
236 @versions = target_projects.map {|p| p.shared_versions.open}.reduce(:&)
236 @versions = target_projects.map {|p| p.shared_versions.open}.reduce(:&)
237 @categories = target_projects.map {|p| p.issue_categories}.reduce(:&)
237 @categories = target_projects.map {|p| p.issue_categories}.reduce(:&)
238 if @copy
238 if @copy
239 @attachments_present = @issues.detect {|i| i.attachments.any?}.present?
239 @attachments_present = @issues.detect {|i| i.attachments.any?}.present?
240 @subtasks_present = @issues.detect {|i| !i.leaf?}.present?
240 @subtasks_present = @issues.detect {|i| !i.leaf?}.present?
241 end
241 end
242
242
243 @safe_attributes = @issues.map(&:safe_attribute_names).reduce(:&)
243 @safe_attributes = @issues.map(&:safe_attribute_names).reduce(:&)
244 render :layout => false if request.xhr?
244 render :layout => false if request.xhr?
245 end
245 end
246
246
247 def bulk_update
247 def bulk_update
248 @issues.sort!
248 @issues.sort!
249 @copy = params[:copy].present?
249 @copy = params[:copy].present?
250 attributes = parse_params_for_bulk_issue_attributes(params)
250 attributes = parse_params_for_bulk_issue_attributes(params)
251
251
252 unsaved_issue_ids = []
252 unsaved_issue_ids = []
253 moved_issues = []
253 moved_issues = []
254
254
255 if @copy && params[:copy_subtasks].present?
255 if @copy && params[:copy_subtasks].present?
256 # Descendant issues will be copied with the parent task
256 # Descendant issues will be copied with the parent task
257 # Don't copy them twice
257 # Don't copy them twice
258 @issues.reject! {|issue| @issues.detect {|other| issue.is_descendant_of?(other)}}
258 @issues.reject! {|issue| @issues.detect {|other| issue.is_descendant_of?(other)}}
259 end
259 end
260
260
261 @issues.each do |issue|
261 @issues.each do |issue|
262 issue.reload
262 issue.reload
263 if @copy
263 if @copy
264 issue = issue.copy({},
264 issue = issue.copy({},
265 :attachments => params[:copy_attachments].present?,
265 :attachments => params[:copy_attachments].present?,
266 :subtasks => params[:copy_subtasks].present?
266 :subtasks => params[:copy_subtasks].present?
267 )
267 )
268 end
268 end
269 journal = issue.init_journal(User.current, params[:notes])
269 journal = issue.init_journal(User.current, params[:notes])
270 issue.safe_attributes = attributes
270 issue.safe_attributes = attributes
271 call_hook(:controller_issues_bulk_edit_before_save, { :params => params, :issue => issue })
271 call_hook(:controller_issues_bulk_edit_before_save, { :params => params, :issue => issue })
272 if issue.save
272 if issue.save
273 moved_issues << issue
273 moved_issues << issue
274 else
274 else
275 logger.info "issue could not be updated or copied: #{issue.errors.full_messages}" if logger && logger.info
275 # Keep unsaved issue ids to display them in flash error
276 # Keep unsaved issue ids to display them in flash error
276 unsaved_issue_ids << issue.id
277 unsaved_issue_ids << issue.id
277 end
278 end
278 end
279 end
279 set_flash_from_bulk_issue_save(@issues, unsaved_issue_ids)
280 set_flash_from_bulk_issue_save(@issues, unsaved_issue_ids)
280
281
281 if params[:follow]
282 if params[:follow]
282 if @issues.size == 1 && moved_issues.size == 1
283 if @issues.size == 1 && moved_issues.size == 1
283 redirect_to issue_path(moved_issues.first)
284 redirect_to issue_path(moved_issues.first)
284 elsif moved_issues.map(&:project).uniq.size == 1
285 elsif moved_issues.map(&:project).uniq.size == 1
285 redirect_to project_issues_path(moved_issues.map(&:project).first)
286 redirect_to project_issues_path(moved_issues.map(&:project).first)
286 end
287 end
287 else
288 else
288 redirect_back_or_default _project_issues_path(@project)
289 redirect_back_or_default _project_issues_path(@project)
289 end
290 end
290 end
291 end
291
292
292 def destroy
293 def destroy
293 @hours = TimeEntry.sum(:hours, :conditions => ['issue_id IN (?)', @issues]).to_f
294 @hours = TimeEntry.sum(:hours, :conditions => ['issue_id IN (?)', @issues]).to_f
294 if @hours > 0
295 if @hours > 0
295 case params[:todo]
296 case params[:todo]
296 when 'destroy'
297 when 'destroy'
297 # nothing to do
298 # nothing to do
298 when 'nullify'
299 when 'nullify'
299 TimeEntry.update_all('issue_id = NULL', ['issue_id IN (?)', @issues])
300 TimeEntry.update_all('issue_id = NULL', ['issue_id IN (?)', @issues])
300 when 'reassign'
301 when 'reassign'
301 reassign_to = @project.issues.find_by_id(params[:reassign_to_id])
302 reassign_to = @project.issues.find_by_id(params[:reassign_to_id])
302 if reassign_to.nil?
303 if reassign_to.nil?
303 flash.now[:error] = l(:error_issue_not_found_in_project)
304 flash.now[:error] = l(:error_issue_not_found_in_project)
304 return
305 return
305 else
306 else
306 TimeEntry.update_all("issue_id = #{reassign_to.id}", ['issue_id IN (?)', @issues])
307 TimeEntry.update_all("issue_id = #{reassign_to.id}", ['issue_id IN (?)', @issues])
307 end
308 end
308 else
309 else
309 # display the destroy form if it's a user request
310 # display the destroy form if it's a user request
310 return unless api_request?
311 return unless api_request?
311 end
312 end
312 end
313 end
313 @issues.each do |issue|
314 @issues.each do |issue|
314 begin
315 begin
315 issue.reload.destroy
316 issue.reload.destroy
316 rescue ::ActiveRecord::RecordNotFound # raised by #reload if issue no longer exists
317 rescue ::ActiveRecord::RecordNotFound # raised by #reload if issue no longer exists
317 # nothing to do, issue was already deleted (eg. by a parent)
318 # nothing to do, issue was already deleted (eg. by a parent)
318 end
319 end
319 end
320 end
320 respond_to do |format|
321 respond_to do |format|
321 format.html { redirect_back_or_default _project_issues_path(@project) }
322 format.html { redirect_back_or_default _project_issues_path(@project) }
322 format.api { render_api_ok }
323 format.api { render_api_ok }
323 end
324 end
324 end
325 end
325
326
326 private
327 private
327
328
328 def find_project
329 def find_project
329 project_id = params[:project_id] || (params[:issue] && params[:issue][:project_id])
330 project_id = params[:project_id] || (params[:issue] && params[:issue][:project_id])
330 @project = Project.find(project_id)
331 @project = Project.find(project_id)
331 rescue ActiveRecord::RecordNotFound
332 rescue ActiveRecord::RecordNotFound
332 render_404
333 render_404
333 end
334 end
334
335
335 def retrieve_previous_and_next_issue_ids
336 def retrieve_previous_and_next_issue_ids
336 retrieve_query_from_session
337 retrieve_query_from_session
337 if @query
338 if @query
338 sort_init(@query.sort_criteria.empty? ? [['id', 'desc']] : @query.sort_criteria)
339 sort_init(@query.sort_criteria.empty? ? [['id', 'desc']] : @query.sort_criteria)
339 sort_update(@query.sortable_columns, 'issues_index_sort')
340 sort_update(@query.sortable_columns, 'issues_index_sort')
340 limit = 500
341 limit = 500
341 issue_ids = @query.issue_ids(:order => sort_clause, :limit => (limit + 1), :include => [:assigned_to, :tracker, :priority, :category, :fixed_version])
342 issue_ids = @query.issue_ids(:order => sort_clause, :limit => (limit + 1), :include => [:assigned_to, :tracker, :priority, :category, :fixed_version])
342 if (idx = issue_ids.index(@issue.id)) && idx < limit
343 if (idx = issue_ids.index(@issue.id)) && idx < limit
343 if issue_ids.size < 500
344 if issue_ids.size < 500
344 @issue_position = idx + 1
345 @issue_position = idx + 1
345 @issue_count = issue_ids.size
346 @issue_count = issue_ids.size
346 end
347 end
347 @prev_issue_id = issue_ids[idx - 1] if idx > 0
348 @prev_issue_id = issue_ids[idx - 1] if idx > 0
348 @next_issue_id = issue_ids[idx + 1] if idx < (issue_ids.size - 1)
349 @next_issue_id = issue_ids[idx + 1] if idx < (issue_ids.size - 1)
349 end
350 end
350 end
351 end
351 end
352 end
352
353
353 # Used by #edit and #update to set some common instance variables
354 # Used by #edit and #update to set some common instance variables
354 # from the params
355 # from the params
355 # TODO: Refactor, not everything in here is needed by #edit
356 # TODO: Refactor, not everything in here is needed by #edit
356 def update_issue_from_params
357 def update_issue_from_params
357 @edit_allowed = User.current.allowed_to?(:edit_issues, @project)
358 @edit_allowed = User.current.allowed_to?(:edit_issues, @project)
358 @time_entry = TimeEntry.new(:issue => @issue, :project => @issue.project)
359 @time_entry = TimeEntry.new(:issue => @issue, :project => @issue.project)
359 @time_entry.attributes = params[:time_entry]
360 @time_entry.attributes = params[:time_entry]
360
361
361 @issue.init_journal(User.current)
362 @issue.init_journal(User.current)
362
363
363 issue_attributes = params[:issue]
364 issue_attributes = params[:issue]
364 if issue_attributes && params[:conflict_resolution]
365 if issue_attributes && params[:conflict_resolution]
365 case params[:conflict_resolution]
366 case params[:conflict_resolution]
366 when 'overwrite'
367 when 'overwrite'
367 issue_attributes = issue_attributes.dup
368 issue_attributes = issue_attributes.dup
368 issue_attributes.delete(:lock_version)
369 issue_attributes.delete(:lock_version)
369 when 'add_notes'
370 when 'add_notes'
370 issue_attributes = issue_attributes.slice(:notes)
371 issue_attributes = issue_attributes.slice(:notes)
371 when 'cancel'
372 when 'cancel'
372 redirect_to issue_path(@issue)
373 redirect_to issue_path(@issue)
373 return false
374 return false
374 end
375 end
375 end
376 end
376 @issue.safe_attributes = issue_attributes
377 @issue.safe_attributes = issue_attributes
377 @priorities = IssuePriority.active
378 @priorities = IssuePriority.active
378 @allowed_statuses = @issue.new_statuses_allowed_to(User.current)
379 @allowed_statuses = @issue.new_statuses_allowed_to(User.current)
379 true
380 true
380 end
381 end
381
382
382 # TODO: Refactor, lots of extra code in here
383 # TODO: Refactor, lots of extra code in here
383 # TODO: Changing tracker on an existing issue should not trigger this
384 # TODO: Changing tracker on an existing issue should not trigger this
384 def build_new_issue_from_params
385 def build_new_issue_from_params
385 if params[:id].blank?
386 if params[:id].blank?
386 @issue = Issue.new
387 @issue = Issue.new
387 if params[:copy_from]
388 if params[:copy_from]
388 begin
389 begin
389 @copy_from = Issue.visible.find(params[:copy_from])
390 @copy_from = Issue.visible.find(params[:copy_from])
390 @copy_attachments = params[:copy_attachments].present? || request.get?
391 @copy_attachments = params[:copy_attachments].present? || request.get?
391 @copy_subtasks = params[:copy_subtasks].present? || request.get?
392 @copy_subtasks = params[:copy_subtasks].present? || request.get?
392 @issue.copy_from(@copy_from, :attachments => @copy_attachments, :subtasks => @copy_subtasks)
393 @issue.copy_from(@copy_from, :attachments => @copy_attachments, :subtasks => @copy_subtasks)
393 rescue ActiveRecord::RecordNotFound
394 rescue ActiveRecord::RecordNotFound
394 render_404
395 render_404
395 return
396 return
396 end
397 end
397 end
398 end
398 @issue.project = @project
399 @issue.project = @project
399 else
400 else
400 @issue = @project.issues.visible.find(params[:id])
401 @issue = @project.issues.visible.find(params[:id])
401 end
402 end
402
403
403 @issue.project = @project
404 @issue.project = @project
404 @issue.author ||= User.current
405 @issue.author ||= User.current
405 # Tracker must be set before custom field values
406 # Tracker must be set before custom field values
406 @issue.tracker ||= @project.trackers.find((params[:issue] && params[:issue][:tracker_id]) || params[:tracker_id] || :first)
407 @issue.tracker ||= @project.trackers.find((params[:issue] && params[:issue][:tracker_id]) || params[:tracker_id] || :first)
407 if @issue.tracker.nil?
408 if @issue.tracker.nil?
408 render_error l(:error_no_tracker_in_project)
409 render_error l(:error_no_tracker_in_project)
409 return false
410 return false
410 end
411 end
411 @issue.start_date ||= Date.today if Setting.default_issue_start_date_to_creation_date?
412 @issue.start_date ||= Date.today if Setting.default_issue_start_date_to_creation_date?
412 @issue.safe_attributes = params[:issue]
413 @issue.safe_attributes = params[:issue]
413
414
414 @priorities = IssuePriority.active
415 @priorities = IssuePriority.active
415 @allowed_statuses = @issue.new_statuses_allowed_to(User.current, true)
416 @allowed_statuses = @issue.new_statuses_allowed_to(User.current, true)
416 @available_watchers = (@issue.project.users.sort + @issue.watcher_users).uniq
417 @available_watchers = (@issue.project.users.sort + @issue.watcher_users).uniq
417 end
418 end
418
419
419 def check_for_default_issue_status
420 def check_for_default_issue_status
420 if IssueStatus.default.nil?
421 if IssueStatus.default.nil?
421 render_error l(:error_no_default_issue_status)
422 render_error l(:error_no_default_issue_status)
422 return false
423 return false
423 end
424 end
424 end
425 end
425
426
426 def parse_params_for_bulk_issue_attributes(params)
427 def parse_params_for_bulk_issue_attributes(params)
427 attributes = (params[:issue] || {}).reject {|k,v| v.blank?}
428 attributes = (params[:issue] || {}).reject {|k,v| v.blank?}
428 attributes.keys.each {|k| attributes[k] = '' if attributes[k] == 'none'}
429 attributes.keys.each {|k| attributes[k] = '' if attributes[k] == 'none'}
429 if custom = attributes[:custom_field_values]
430 if custom = attributes[:custom_field_values]
430 custom.reject! {|k,v| v.blank?}
431 custom.reject! {|k,v| v.blank?}
431 custom.keys.each do |k|
432 custom.keys.each do |k|
432 if custom[k].is_a?(Array)
433 if custom[k].is_a?(Array)
433 custom[k] << '' if custom[k].delete('__none__')
434 custom[k] << '' if custom[k].delete('__none__')
434 else
435 else
435 custom[k] = '' if custom[k] == '__none__'
436 custom[k] = '' if custom[k] == '__none__'
436 end
437 end
437 end
438 end
438 end
439 end
439 attributes
440 attributes
440 end
441 end
441 end
442 end
@@ -1,314 +1,315
1 # Redmine - project management software
1 # Redmine - project management software
2 # Copyright (C) 2006-2013 Jean-Philippe Lang
2 # Copyright (C) 2006-2013 Jean-Philippe Lang
3 #
3 #
4 # This program is free software; you can redistribute it and/or
4 # This program is free software; you can redistribute it and/or
5 # modify it under the terms of the GNU General Public License
5 # modify it under the terms of the GNU General Public License
6 # as published by the Free Software Foundation; either version 2
6 # as published by the Free Software Foundation; either version 2
7 # of the License, or (at your option) any later version.
7 # of the License, or (at your option) any later version.
8 #
8 #
9 # This program is distributed in the hope that it will be useful,
9 # This program is distributed in the hope that it will be useful,
10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 # GNU General Public License for more details.
12 # GNU General Public License for more details.
13 #
13 #
14 # You should have received a copy of the GNU General Public License
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
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.
16 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
17
17
18 class TimelogController < ApplicationController
18 class TimelogController < ApplicationController
19 menu_item :issues
19 menu_item :issues
20
20
21 before_filter :find_project_for_new_time_entry, :only => [:create]
21 before_filter :find_project_for_new_time_entry, :only => [:create]
22 before_filter :find_time_entry, :only => [:show, :edit, :update]
22 before_filter :find_time_entry, :only => [:show, :edit, :update]
23 before_filter :find_time_entries, :only => [:bulk_edit, :bulk_update, :destroy]
23 before_filter :find_time_entries, :only => [:bulk_edit, :bulk_update, :destroy]
24 before_filter :authorize, :except => [:new, :index, :report]
24 before_filter :authorize, :except => [:new, :index, :report]
25
25
26 before_filter :find_optional_project, :only => [:index, :report]
26 before_filter :find_optional_project, :only => [:index, :report]
27 before_filter :find_optional_project_for_new_time_entry, :only => [:new]
27 before_filter :find_optional_project_for_new_time_entry, :only => [:new]
28 before_filter :authorize_global, :only => [:new, :index, :report]
28 before_filter :authorize_global, :only => [:new, :index, :report]
29
29
30 accept_rss_auth :index
30 accept_rss_auth :index
31 accept_api_auth :index, :show, :create, :update, :destroy
31 accept_api_auth :index, :show, :create, :update, :destroy
32
32
33 rescue_from Query::StatementInvalid, :with => :query_statement_invalid
33 rescue_from Query::StatementInvalid, :with => :query_statement_invalid
34
34
35 helper :sort
35 helper :sort
36 include SortHelper
36 include SortHelper
37 helper :issues
37 helper :issues
38 include TimelogHelper
38 include TimelogHelper
39 helper :custom_fields
39 helper :custom_fields
40 include CustomFieldsHelper
40 include CustomFieldsHelper
41 helper :queries
41 helper :queries
42 include QueriesHelper
42 include QueriesHelper
43
43
44 def index
44 def index
45 @query = TimeEntryQuery.build_from_params(params, :project => @project, :name => '_')
45 @query = TimeEntryQuery.build_from_params(params, :project => @project, :name => '_')
46 scope = time_entry_scope
46 scope = time_entry_scope
47
47
48 sort_init(@query.sort_criteria.empty? ? [['spent_on', 'desc']] : @query.sort_criteria)
48 sort_init(@query.sort_criteria.empty? ? [['spent_on', 'desc']] : @query.sort_criteria)
49 sort_update(@query.sortable_columns)
49 sort_update(@query.sortable_columns)
50
50
51 respond_to do |format|
51 respond_to do |format|
52 format.html {
52 format.html {
53 # Paginate results
53 # Paginate results
54 @entry_count = scope.count
54 @entry_count = scope.count
55 @entry_pages = Paginator.new @entry_count, per_page_option, params['page']
55 @entry_pages = Paginator.new @entry_count, per_page_option, params['page']
56 @entries = scope.all(
56 @entries = scope.all(
57 :include => [:project, :activity, :user, {:issue => :tracker}],
57 :include => [:project, :activity, :user, {:issue => :tracker}],
58 :order => sort_clause,
58 :order => sort_clause,
59 :limit => @entry_pages.per_page,
59 :limit => @entry_pages.per_page,
60 :offset => @entry_pages.offset
60 :offset => @entry_pages.offset
61 )
61 )
62 @total_hours = scope.sum(:hours).to_f
62 @total_hours = scope.sum(:hours).to_f
63
63
64 render :layout => !request.xhr?
64 render :layout => !request.xhr?
65 }
65 }
66 format.api {
66 format.api {
67 @entry_count = scope.count
67 @entry_count = scope.count
68 @offset, @limit = api_offset_and_limit
68 @offset, @limit = api_offset_and_limit
69 @entries = scope.all(
69 @entries = scope.all(
70 :include => [:project, :activity, :user, {:issue => :tracker}],
70 :include => [:project, :activity, :user, {:issue => :tracker}],
71 :order => sort_clause,
71 :order => sort_clause,
72 :limit => @limit,
72 :limit => @limit,
73 :offset => @offset
73 :offset => @offset
74 )
74 )
75 }
75 }
76 format.atom {
76 format.atom {
77 entries = scope.all(
77 entries = scope.all(
78 :include => [:project, :activity, :user, {:issue => :tracker}],
78 :include => [:project, :activity, :user, {:issue => :tracker}],
79 :order => "#{TimeEntry.table_name}.created_on DESC",
79 :order => "#{TimeEntry.table_name}.created_on DESC",
80 :limit => Setting.feeds_limit.to_i
80 :limit => Setting.feeds_limit.to_i
81 )
81 )
82 render_feed(entries, :title => l(:label_spent_time))
82 render_feed(entries, :title => l(:label_spent_time))
83 }
83 }
84 format.csv {
84 format.csv {
85 # Export all entries
85 # Export all entries
86 @entries = scope.all(
86 @entries = scope.all(
87 :include => [:project, :activity, :user, {:issue => [:tracker, :assigned_to, :priority]}],
87 :include => [:project, :activity, :user, {:issue => [:tracker, :assigned_to, :priority]}],
88 :order => sort_clause
88 :order => sort_clause
89 )
89 )
90 send_data(query_to_csv(@entries, @query, params), :type => 'text/csv; header=present', :filename => 'timelog.csv')
90 send_data(query_to_csv(@entries, @query, params), :type => 'text/csv; header=present', :filename => 'timelog.csv')
91 }
91 }
92 end
92 end
93 end
93 end
94
94
95 def report
95 def report
96 @query = TimeEntryQuery.build_from_params(params, :project => @project, :name => '_')
96 @query = TimeEntryQuery.build_from_params(params, :project => @project, :name => '_')
97 scope = time_entry_scope
97 scope = time_entry_scope
98
98
99 @report = Redmine::Helpers::TimeReport.new(@project, @issue, params[:criteria], params[:columns], scope)
99 @report = Redmine::Helpers::TimeReport.new(@project, @issue, params[:criteria], params[:columns], scope)
100
100
101 respond_to do |format|
101 respond_to do |format|
102 format.html { render :layout => !request.xhr? }
102 format.html { render :layout => !request.xhr? }
103 format.csv { send_data(report_to_csv(@report), :type => 'text/csv; header=present', :filename => 'timelog.csv') }
103 format.csv { send_data(report_to_csv(@report), :type => 'text/csv; header=present', :filename => 'timelog.csv') }
104 end
104 end
105 end
105 end
106
106
107 def show
107 def show
108 respond_to do |format|
108 respond_to do |format|
109 # TODO: Implement html response
109 # TODO: Implement html response
110 format.html { render :nothing => true, :status => 406 }
110 format.html { render :nothing => true, :status => 406 }
111 format.api
111 format.api
112 end
112 end
113 end
113 end
114
114
115 def new
115 def new
116 @time_entry ||= TimeEntry.new(:project => @project, :issue => @issue, :user => User.current, :spent_on => User.current.today)
116 @time_entry ||= TimeEntry.new(:project => @project, :issue => @issue, :user => User.current, :spent_on => User.current.today)
117 @time_entry.safe_attributes = params[:time_entry]
117 @time_entry.safe_attributes = params[:time_entry]
118 end
118 end
119
119
120 def create
120 def create
121 @time_entry ||= TimeEntry.new(:project => @project, :issue => @issue, :user => User.current, :spent_on => User.current.today)
121 @time_entry ||= TimeEntry.new(:project => @project, :issue => @issue, :user => User.current, :spent_on => User.current.today)
122 @time_entry.safe_attributes = params[:time_entry]
122 @time_entry.safe_attributes = params[:time_entry]
123
123
124 call_hook(:controller_timelog_edit_before_save, { :params => params, :time_entry => @time_entry })
124 call_hook(:controller_timelog_edit_before_save, { :params => params, :time_entry => @time_entry })
125
125
126 if @time_entry.save
126 if @time_entry.save
127 respond_to do |format|
127 respond_to do |format|
128 format.html {
128 format.html {
129 flash[:notice] = l(:notice_successful_create)
129 flash[:notice] = l(:notice_successful_create)
130 if params[:continue]
130 if params[:continue]
131 if params[:project_id]
131 if params[:project_id]
132 options = {
132 options = {
133 :time_entry => {:issue_id => @time_entry.issue_id, :activity_id => @time_entry.activity_id},
133 :time_entry => {:issue_id => @time_entry.issue_id, :activity_id => @time_entry.activity_id},
134 :back_url => params[:back_url]
134 :back_url => params[:back_url]
135 }
135 }
136 if @time_entry.issue
136 if @time_entry.issue
137 redirect_to new_project_issue_time_entry_path(@time_entry.project, @time_entry.issue, options)
137 redirect_to new_project_issue_time_entry_path(@time_entry.project, @time_entry.issue, options)
138 else
138 else
139 redirect_to new_project_time_entry_path(@time_entry.project, options)
139 redirect_to new_project_time_entry_path(@time_entry.project, options)
140 end
140 end
141 else
141 else
142 options = {
142 options = {
143 :time_entry => {:project_id => @time_entry.project_id, :issue_id => @time_entry.issue_id, :activity_id => @time_entry.activity_id},
143 :time_entry => {:project_id => @time_entry.project_id, :issue_id => @time_entry.issue_id, :activity_id => @time_entry.activity_id},
144 :back_url => params[:back_url]
144 :back_url => params[:back_url]
145 }
145 }
146 redirect_to new_time_entry_path(options)
146 redirect_to new_time_entry_path(options)
147 end
147 end
148 else
148 else
149 redirect_back_or_default project_time_entries_path(@time_entry.project)
149 redirect_back_or_default project_time_entries_path(@time_entry.project)
150 end
150 end
151 }
151 }
152 format.api { render :action => 'show', :status => :created, :location => time_entry_url(@time_entry) }
152 format.api { render :action => 'show', :status => :created, :location => time_entry_url(@time_entry) }
153 end
153 end
154 else
154 else
155 respond_to do |format|
155 respond_to do |format|
156 format.html { render :action => 'new' }
156 format.html { render :action => 'new' }
157 format.api { render_validation_errors(@time_entry) }
157 format.api { render_validation_errors(@time_entry) }
158 end
158 end
159 end
159 end
160 end
160 end
161
161
162 def edit
162 def edit
163 @time_entry.safe_attributes = params[:time_entry]
163 @time_entry.safe_attributes = params[:time_entry]
164 end
164 end
165
165
166 def update
166 def update
167 @time_entry.safe_attributes = params[:time_entry]
167 @time_entry.safe_attributes = params[:time_entry]
168
168
169 call_hook(:controller_timelog_edit_before_save, { :params => params, :time_entry => @time_entry })
169 call_hook(:controller_timelog_edit_before_save, { :params => params, :time_entry => @time_entry })
170
170
171 if @time_entry.save
171 if @time_entry.save
172 respond_to do |format|
172 respond_to do |format|
173 format.html {
173 format.html {
174 flash[:notice] = l(:notice_successful_update)
174 flash[:notice] = l(:notice_successful_update)
175 redirect_back_or_default project_time_entries_path(@time_entry.project)
175 redirect_back_or_default project_time_entries_path(@time_entry.project)
176 }
176 }
177 format.api { render_api_ok }
177 format.api { render_api_ok }
178 end
178 end
179 else
179 else
180 respond_to do |format|
180 respond_to do |format|
181 format.html { render :action => 'edit' }
181 format.html { render :action => 'edit' }
182 format.api { render_validation_errors(@time_entry) }
182 format.api { render_validation_errors(@time_entry) }
183 end
183 end
184 end
184 end
185 end
185 end
186
186
187 def bulk_edit
187 def bulk_edit
188 @available_activities = TimeEntryActivity.shared.active
188 @available_activities = TimeEntryActivity.shared.active
189 @custom_fields = TimeEntry.first.available_custom_fields
189 @custom_fields = TimeEntry.first.available_custom_fields
190 end
190 end
191
191
192 def bulk_update
192 def bulk_update
193 attributes = parse_params_for_bulk_time_entry_attributes(params)
193 attributes = parse_params_for_bulk_time_entry_attributes(params)
194
194
195 unsaved_time_entry_ids = []
195 unsaved_time_entry_ids = []
196 @time_entries.each do |time_entry|
196 @time_entries.each do |time_entry|
197 time_entry.reload
197 time_entry.reload
198 time_entry.safe_attributes = attributes
198 time_entry.safe_attributes = attributes
199 call_hook(:controller_time_entries_bulk_edit_before_save, { :params => params, :time_entry => time_entry })
199 call_hook(:controller_time_entries_bulk_edit_before_save, { :params => params, :time_entry => time_entry })
200 unless time_entry.save
200 unless time_entry.save
201 logger.info "time entry could not be updated: #{time_entry.errors.full_messages}" if logger && logger.info
201 # Keep unsaved time_entry ids to display them in flash error
202 # Keep unsaved time_entry ids to display them in flash error
202 unsaved_time_entry_ids << time_entry.id
203 unsaved_time_entry_ids << time_entry.id
203 end
204 end
204 end
205 end
205 set_flash_from_bulk_time_entry_save(@time_entries, unsaved_time_entry_ids)
206 set_flash_from_bulk_time_entry_save(@time_entries, unsaved_time_entry_ids)
206 redirect_back_or_default project_time_entries_path(@projects.first)
207 redirect_back_or_default project_time_entries_path(@projects.first)
207 end
208 end
208
209
209 def destroy
210 def destroy
210 destroyed = TimeEntry.transaction do
211 destroyed = TimeEntry.transaction do
211 @time_entries.each do |t|
212 @time_entries.each do |t|
212 unless t.destroy && t.destroyed?
213 unless t.destroy && t.destroyed?
213 raise ActiveRecord::Rollback
214 raise ActiveRecord::Rollback
214 end
215 end
215 end
216 end
216 end
217 end
217
218
218 respond_to do |format|
219 respond_to do |format|
219 format.html {
220 format.html {
220 if destroyed
221 if destroyed
221 flash[:notice] = l(:notice_successful_delete)
222 flash[:notice] = l(:notice_successful_delete)
222 else
223 else
223 flash[:error] = l(:notice_unable_delete_time_entry)
224 flash[:error] = l(:notice_unable_delete_time_entry)
224 end
225 end
225 redirect_back_or_default project_time_entries_path(@projects.first)
226 redirect_back_or_default project_time_entries_path(@projects.first)
226 }
227 }
227 format.api {
228 format.api {
228 if destroyed
229 if destroyed
229 render_api_ok
230 render_api_ok
230 else
231 else
231 render_validation_errors(@time_entries)
232 render_validation_errors(@time_entries)
232 end
233 end
233 }
234 }
234 end
235 end
235 end
236 end
236
237
237 private
238 private
238 def find_time_entry
239 def find_time_entry
239 @time_entry = TimeEntry.find(params[:id])
240 @time_entry = TimeEntry.find(params[:id])
240 unless @time_entry.editable_by?(User.current)
241 unless @time_entry.editable_by?(User.current)
241 render_403
242 render_403
242 return false
243 return false
243 end
244 end
244 @project = @time_entry.project
245 @project = @time_entry.project
245 rescue ActiveRecord::RecordNotFound
246 rescue ActiveRecord::RecordNotFound
246 render_404
247 render_404
247 end
248 end
248
249
249 def find_time_entries
250 def find_time_entries
250 @time_entries = TimeEntry.find_all_by_id(params[:id] || params[:ids])
251 @time_entries = TimeEntry.find_all_by_id(params[:id] || params[:ids])
251 raise ActiveRecord::RecordNotFound if @time_entries.empty?
252 raise ActiveRecord::RecordNotFound if @time_entries.empty?
252 @projects = @time_entries.collect(&:project).compact.uniq
253 @projects = @time_entries.collect(&:project).compact.uniq
253 @project = @projects.first if @projects.size == 1
254 @project = @projects.first if @projects.size == 1
254 rescue ActiveRecord::RecordNotFound
255 rescue ActiveRecord::RecordNotFound
255 render_404
256 render_404
256 end
257 end
257
258
258 def set_flash_from_bulk_time_entry_save(time_entries, unsaved_time_entry_ids)
259 def set_flash_from_bulk_time_entry_save(time_entries, unsaved_time_entry_ids)
259 if unsaved_time_entry_ids.empty?
260 if unsaved_time_entry_ids.empty?
260 flash[:notice] = l(:notice_successful_update) unless time_entries.empty?
261 flash[:notice] = l(:notice_successful_update) unless time_entries.empty?
261 else
262 else
262 flash[:error] = l(:notice_failed_to_save_time_entries,
263 flash[:error] = l(:notice_failed_to_save_time_entries,
263 :count => unsaved_time_entry_ids.size,
264 :count => unsaved_time_entry_ids.size,
264 :total => time_entries.size,
265 :total => time_entries.size,
265 :ids => '#' + unsaved_time_entry_ids.join(', #'))
266 :ids => '#' + unsaved_time_entry_ids.join(', #'))
266 end
267 end
267 end
268 end
268
269
269 def find_optional_project_for_new_time_entry
270 def find_optional_project_for_new_time_entry
270 if (project_id = (params[:project_id] || params[:time_entry] && params[:time_entry][:project_id])).present?
271 if (project_id = (params[:project_id] || params[:time_entry] && params[:time_entry][:project_id])).present?
271 @project = Project.find(project_id)
272 @project = Project.find(project_id)
272 end
273 end
273 if (issue_id = (params[:issue_id] || params[:time_entry] && params[:time_entry][:issue_id])).present?
274 if (issue_id = (params[:issue_id] || params[:time_entry] && params[:time_entry][:issue_id])).present?
274 @issue = Issue.find(issue_id)
275 @issue = Issue.find(issue_id)
275 @project ||= @issue.project
276 @project ||= @issue.project
276 end
277 end
277 rescue ActiveRecord::RecordNotFound
278 rescue ActiveRecord::RecordNotFound
278 render_404
279 render_404
279 end
280 end
280
281
281 def find_project_for_new_time_entry
282 def find_project_for_new_time_entry
282 find_optional_project_for_new_time_entry
283 find_optional_project_for_new_time_entry
283 if @project.nil?
284 if @project.nil?
284 render_404
285 render_404
285 end
286 end
286 end
287 end
287
288
288 def find_optional_project
289 def find_optional_project
289 if !params[:issue_id].blank?
290 if !params[:issue_id].blank?
290 @issue = Issue.find(params[:issue_id])
291 @issue = Issue.find(params[:issue_id])
291 @project = @issue.project
292 @project = @issue.project
292 elsif !params[:project_id].blank?
293 elsif !params[:project_id].blank?
293 @project = Project.find(params[:project_id])
294 @project = Project.find(params[:project_id])
294 end
295 end
295 end
296 end
296
297
297 # Returns the TimeEntry scope for index and report actions
298 # Returns the TimeEntry scope for index and report actions
298 def time_entry_scope
299 def time_entry_scope
299 scope = TimeEntry.visible.where(@query.statement)
300 scope = TimeEntry.visible.where(@query.statement)
300 if @issue
301 if @issue
301 scope = scope.on_issue(@issue)
302 scope = scope.on_issue(@issue)
302 elsif @project
303 elsif @project
303 scope = scope.on_project(@project, Setting.display_subprojects_issues?)
304 scope = scope.on_project(@project, Setting.display_subprojects_issues?)
304 end
305 end
305 scope
306 scope
306 end
307 end
307
308
308 def parse_params_for_bulk_time_entry_attributes(params)
309 def parse_params_for_bulk_time_entry_attributes(params)
309 attributes = (params[:time_entry] || {}).reject {|k,v| v.blank?}
310 attributes = (params[:time_entry] || {}).reject {|k,v| v.blank?}
310 attributes.keys.each {|k| attributes[k] = '' if attributes[k] == 'none'}
311 attributes.keys.each {|k| attributes[k] = '' if attributes[k] == 'none'}
311 attributes[:custom_field_values].reject! {|k,v| v.blank?} if attributes[:custom_field_values]
312 attributes[:custom_field_values].reject! {|k,v| v.blank?} if attributes[:custom_field_values]
312 attributes
313 attributes
313 end
314 end
314 end
315 end
General Comments 0
You need to be logged in to leave comments. Login now