##// END OF EJS Templates
Update project_id column in case some time entries were on subtasks of different projects (#24693)....
Jean-Philippe Lang -
r15738:1b3042235493
parent child
Show More
@@ -1,575 +1,575
1 1 # Redmine - project management software
2 2 # Copyright (C) 2006-2016 Jean-Philippe Lang
3 3 #
4 4 # This program is free software; you can redistribute it and/or
5 5 # modify it under the terms of the GNU General Public License
6 6 # as published by the Free Software Foundation; either version 2
7 7 # of the License, or (at your option) any later version.
8 8 #
9 9 # This program is distributed in the hope that it will be useful,
10 10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
11 11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 12 # GNU General Public License for more details.
13 13 #
14 14 # You should have received a copy of the GNU General Public License
15 15 # along with this program; if not, write to the Free Software
16 16 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
17 17
18 18 class IssuesController < ApplicationController
19 19 default_search_scope :issues
20 20
21 21 before_action :find_issue, :only => [:show, :edit, :update]
22 22 before_action :find_issues, :only => [:bulk_edit, :bulk_update, :destroy]
23 23 before_action :authorize, :except => [:index, :new, :create]
24 24 before_action :find_optional_project, :only => [:index, :new, :create]
25 25 before_action :build_new_issue_from_params, :only => [:new, :create]
26 26 accept_rss_auth :index, :show
27 27 accept_api_auth :index, :show, :create, :update, :destroy
28 28
29 29 rescue_from Query::StatementInvalid, :with => :query_statement_invalid
30 30
31 31 helper :journals
32 32 helper :projects
33 33 helper :custom_fields
34 34 helper :issue_relations
35 35 helper :watchers
36 36 helper :attachments
37 37 helper :queries
38 38 include QueriesHelper
39 39 helper :repositories
40 40 helper :sort
41 41 include SortHelper
42 42 helper :timelog
43 43
44 44 def index
45 45 retrieve_query
46 46 sort_init(@query.sort_criteria.empty? ? [['id', 'desc']] : @query.sort_criteria)
47 47 sort_update(@query.sortable_columns)
48 48 @query.sort_criteria = sort_criteria.to_a
49 49
50 50 if @query.valid?
51 51 case params[:format]
52 52 when 'csv', 'pdf'
53 53 @limit = Setting.issues_export_limit.to_i
54 54 if params[:columns] == 'all'
55 55 @query.column_names = @query.available_inline_columns.map(&:name)
56 56 end
57 57 when 'atom'
58 58 @limit = Setting.feeds_limit.to_i
59 59 when 'xml', 'json'
60 60 @offset, @limit = api_offset_and_limit
61 61 @query.column_names = %w(author)
62 62 else
63 63 @limit = per_page_option
64 64 end
65 65
66 66 @issue_count = @query.issue_count
67 67 @issue_pages = Paginator.new @issue_count, @limit, params['page']
68 68 @offset ||= @issue_pages.offset
69 69 @issues = @query.issues(:include => [:assigned_to, :tracker, :priority, :category, :fixed_version],
70 70 :order => sort_clause,
71 71 :offset => @offset,
72 72 :limit => @limit)
73 73 @issue_count_by_group = @query.issue_count_by_group
74 74
75 75 respond_to do |format|
76 76 format.html { render :template => 'issues/index', :layout => !request.xhr? }
77 77 format.api {
78 78 Issue.load_visible_relations(@issues) if include_in_api_response?('relations')
79 79 }
80 80 format.atom { render_feed(@issues, :title => "#{@project || Setting.app_title}: #{l(:label_issue_plural)}") }
81 81 format.csv { send_data(query_to_csv(@issues, @query, params[:csv]), :type => 'text/csv; header=present', :filename => 'issues.csv') }
82 82 format.pdf { send_file_headers! :type => 'application/pdf', :filename => 'issues.pdf' }
83 83 end
84 84 else
85 85 respond_to do |format|
86 86 format.html { render(:template => 'issues/index', :layout => !request.xhr?) }
87 87 format.any(:atom, :csv, :pdf) { head 422 }
88 88 format.api { render_validation_errors(@query) }
89 89 end
90 90 end
91 91 rescue ActiveRecord::RecordNotFound
92 92 render_404
93 93 end
94 94
95 95 def show
96 96 @journals = @issue.journals.
97 97 preload(:details).
98 98 preload(:user => :email_address).
99 99 reorder(:created_on, :id).to_a
100 100 @journals.each_with_index {|j,i| j.indice = i+1}
101 101 @journals.reject!(&:private_notes?) unless User.current.allowed_to?(:view_private_notes, @issue.project)
102 102 Journal.preload_journals_details_custom_fields(@journals)
103 103 @journals.select! {|journal| journal.notes? || journal.visible_details.any?}
104 104 @journals.reverse! if User.current.wants_comments_in_reverse_order?
105 105
106 106 @changesets = @issue.changesets.visible.preload(:repository, :user).to_a
107 107 @changesets.reverse! if User.current.wants_comments_in_reverse_order?
108 108
109 109 @relations = @issue.relations.select {|r| r.other_issue(@issue) && r.other_issue(@issue).visible? }
110 110 @allowed_statuses = @issue.new_statuses_allowed_to(User.current)
111 111 @priorities = IssuePriority.active
112 112 @time_entry = TimeEntry.new(:issue => @issue, :project => @issue.project)
113 113 @relation = IssueRelation.new
114 114
115 115 respond_to do |format|
116 116 format.html {
117 117 retrieve_previous_and_next_issue_ids
118 118 render :template => 'issues/show'
119 119 }
120 120 format.api
121 121 format.atom { render :template => 'journals/index', :layout => false, :content_type => 'application/atom+xml' }
122 122 format.pdf {
123 123 send_file_headers! :type => 'application/pdf', :filename => "#{@project.identifier}-#{@issue.id}.pdf"
124 124 }
125 125 end
126 126 end
127 127
128 128 def new
129 129 respond_to do |format|
130 130 format.html { render :action => 'new', :layout => !request.xhr? }
131 131 format.js
132 132 end
133 133 end
134 134
135 135 def create
136 136 unless User.current.allowed_to?(:add_issues, @issue.project, :global => true)
137 137 raise ::Unauthorized
138 138 end
139 139 call_hook(:controller_issues_new_before_save, { :params => params, :issue => @issue })
140 140 @issue.save_attachments(params[:attachments] || (params[:issue] && params[:issue][:uploads]))
141 141 if @issue.save
142 142 call_hook(:controller_issues_new_after_save, { :params => params, :issue => @issue})
143 143 respond_to do |format|
144 144 format.html {
145 145 render_attachment_warning_if_needed(@issue)
146 146 flash[:notice] = l(:notice_issue_successful_create, :id => view_context.link_to("##{@issue.id}", issue_path(@issue), :title => @issue.subject))
147 147 redirect_after_create
148 148 }
149 149 format.api { render :action => 'show', :status => :created, :location => issue_url(@issue) }
150 150 end
151 151 return
152 152 else
153 153 respond_to do |format|
154 154 format.html {
155 155 if @issue.project.nil?
156 156 render_error :status => 422
157 157 else
158 158 render :action => 'new'
159 159 end
160 160 }
161 161 format.api { render_validation_errors(@issue) }
162 162 end
163 163 end
164 164 end
165 165
166 166 def edit
167 167 return unless update_issue_from_params
168 168
169 169 respond_to do |format|
170 170 format.html { }
171 171 format.js
172 172 end
173 173 end
174 174
175 175 def update
176 176 return unless update_issue_from_params
177 177 @issue.save_attachments(params[:attachments] || (params[:issue] && params[:issue][:uploads]))
178 178 saved = false
179 179 begin
180 180 saved = save_issue_with_child_records
181 181 rescue ActiveRecord::StaleObjectError
182 182 @conflict = true
183 183 if params[:last_journal_id]
184 184 @conflict_journals = @issue.journals_after(params[:last_journal_id]).to_a
185 185 @conflict_journals.reject!(&:private_notes?) unless User.current.allowed_to?(:view_private_notes, @issue.project)
186 186 end
187 187 end
188 188
189 189 if saved
190 190 render_attachment_warning_if_needed(@issue)
191 191 flash[:notice] = l(:notice_successful_update) unless @issue.current_journal.new_record?
192 192
193 193 respond_to do |format|
194 194 format.html { redirect_back_or_default issue_path(@issue, previous_and_next_issue_ids_params) }
195 195 format.api { render_api_ok }
196 196 end
197 197 else
198 198 respond_to do |format|
199 199 format.html { render :action => 'edit' }
200 200 format.api { render_validation_errors(@issue) }
201 201 end
202 202 end
203 203 end
204 204
205 205 # Bulk edit/copy a set of issues
206 206 def bulk_edit
207 207 @issues.sort!
208 208 @copy = params[:copy].present?
209 209 @notes = params[:notes]
210 210
211 211 if @copy
212 212 unless User.current.allowed_to?(:copy_issues, @projects)
213 213 raise ::Unauthorized
214 214 end
215 215 else
216 216 unless @issues.all?(&:attributes_editable?)
217 217 raise ::Unauthorized
218 218 end
219 219 end
220 220
221 221 edited_issues = Issue.where(:id => @issues.map(&:id)).to_a
222 222
223 223 @allowed_projects = Issue.allowed_target_projects
224 224 if params[:issue]
225 225 @target_project = @allowed_projects.detect {|p| p.id.to_s == params[:issue][:project_id].to_s}
226 226 if @target_project
227 227 target_projects = [@target_project]
228 228 edited_issues.each {|issue| issue.project = @target_project}
229 229 end
230 230 end
231 231 target_projects ||= @projects
232 232
233 233 @trackers = target_projects.map {|p| Issue.allowed_target_trackers(p) }.reduce(:&)
234 234 if params[:issue]
235 235 @target_tracker = @trackers.detect {|t| t.id.to_s == params[:issue][:tracker_id].to_s}
236 236 if @target_tracker
237 237 edited_issues.each {|issue| issue.tracker = @target_tracker}
238 238 end
239 239 end
240 240
241 241 if @copy
242 242 # Copied issues will get their default statuses
243 243 @available_statuses = []
244 244 else
245 245 @available_statuses = edited_issues.map(&:new_statuses_allowed_to).reduce(:&)
246 246 end
247 247 if params[:issue]
248 248 @target_status = @available_statuses.detect {|t| t.id.to_s == params[:issue][:status_id].to_s}
249 249 if @target_status
250 250 edited_issues.each {|issue| issue.status = @target_status}
251 251 end
252 252 end
253 253
254 254 @custom_fields = edited_issues.map{|i|i.editable_custom_fields}.reduce(:&).select {|field| field.format.bulk_edit_supported}
255 255 @assignables = target_projects.map(&:assignable_users).reduce(:&)
256 256 @versions = target_projects.map {|p| p.shared_versions.open}.reduce(:&)
257 257 @categories = target_projects.map {|p| p.issue_categories}.reduce(:&)
258 258 if @copy
259 259 @attachments_present = @issues.detect {|i| i.attachments.any?}.present?
260 260 @subtasks_present = @issues.detect {|i| !i.leaf?}.present?
261 261 end
262 262
263 263 @safe_attributes = edited_issues.map(&:safe_attribute_names).reduce(:&)
264 264
265 265 @issue_params = params[:issue] || {}
266 266 @issue_params[:custom_field_values] ||= {}
267 267 end
268 268
269 269 def bulk_update
270 270 @issues.sort!
271 271 @copy = params[:copy].present?
272 272
273 273 attributes = parse_params_for_bulk_update(params[:issue])
274 274 copy_subtasks = (params[:copy_subtasks] == '1')
275 275 copy_attachments = (params[:copy_attachments] == '1')
276 276
277 277 if @copy
278 278 unless User.current.allowed_to?(:copy_issues, @projects)
279 279 raise ::Unauthorized
280 280 end
281 281 target_projects = @projects
282 282 if attributes['project_id'].present?
283 283 target_projects = Project.where(:id => attributes['project_id']).to_a
284 284 end
285 285 unless User.current.allowed_to?(:add_issues, target_projects)
286 286 raise ::Unauthorized
287 287 end
288 288 else
289 289 unless @issues.all?(&:attributes_editable?)
290 290 raise ::Unauthorized
291 291 end
292 292 end
293 293
294 294 unsaved_issues = []
295 295 saved_issues = []
296 296
297 297 if @copy && copy_subtasks
298 298 # Descendant issues will be copied with the parent task
299 299 # Don't copy them twice
300 300 @issues.reject! {|issue| @issues.detect {|other| issue.is_descendant_of?(other)}}
301 301 end
302 302
303 303 @issues.each do |orig_issue|
304 304 orig_issue.reload
305 305 if @copy
306 306 issue = orig_issue.copy({},
307 307 :attachments => copy_attachments,
308 308 :subtasks => copy_subtasks,
309 309 :link => link_copy?(params[:link_copy])
310 310 )
311 311 else
312 312 issue = orig_issue
313 313 end
314 314 journal = issue.init_journal(User.current, params[:notes])
315 315 issue.safe_attributes = attributes
316 316 call_hook(:controller_issues_bulk_edit_before_save, { :params => params, :issue => issue })
317 317 if issue.save
318 318 saved_issues << issue
319 319 else
320 320 unsaved_issues << orig_issue
321 321 end
322 322 end
323 323
324 324 if unsaved_issues.empty?
325 325 flash[:notice] = l(:notice_successful_update) unless saved_issues.empty?
326 326 if params[:follow]
327 327 if @issues.size == 1 && saved_issues.size == 1
328 328 redirect_to issue_path(saved_issues.first)
329 329 elsif saved_issues.map(&:project).uniq.size == 1
330 330 redirect_to project_issues_path(saved_issues.map(&:project).first)
331 331 end
332 332 else
333 333 redirect_back_or_default _project_issues_path(@project)
334 334 end
335 335 else
336 336 @saved_issues = @issues
337 337 @unsaved_issues = unsaved_issues
338 338 @issues = Issue.visible.where(:id => @unsaved_issues.map(&:id)).to_a
339 339 bulk_edit
340 340 render :action => 'bulk_edit'
341 341 end
342 342 end
343 343
344 344 def destroy
345 345 raise Unauthorized unless @issues.all?(&:deletable?)
346 346
347 347 # all issues and their descendants are about to be deleted
348 348 issues_and_descendants_ids = Issue.self_and_descendants(@issues).pluck(:id)
349 349 time_entries = TimeEntry.where(:issue_id => issues_and_descendants_ids)
350 350 @hours = time_entries.sum(:hours).to_f
351 351
352 352 if @hours > 0
353 353 case params[:todo]
354 354 when 'destroy'
355 355 # nothing to do
356 356 when 'nullify'
357 357 time_entries.update_all(:issue_id => nil)
358 358 when 'reassign'
359 359 reassign_to = @project.issues.find_by_id(params[:reassign_to_id])
360 360 if reassign_to.nil?
361 361 flash.now[:error] = l(:error_issue_not_found_in_project)
362 362 return
363 363 elsif issues_and_descendants_ids.include?(reassign_to.id)
364 364 flash.now[:error] = l(:error_cannot_reassign_time_entries_to_an_issue_about_to_be_deleted)
365 365 return
366 366 else
367 time_entries.update_all(:issue_id => reassign_to.id)
367 time_entries.update_all(:issue_id => reassign_to.id, :project_id => reassign_to.project_id)
368 368 end
369 369 else
370 370 # display the destroy form if it's a user request
371 371 return unless api_request?
372 372 end
373 373 end
374 374 @issues.each do |issue|
375 375 begin
376 376 issue.reload.destroy
377 377 rescue ::ActiveRecord::RecordNotFound # raised by #reload if issue no longer exists
378 378 # nothing to do, issue was already deleted (eg. by a parent)
379 379 end
380 380 end
381 381 respond_to do |format|
382 382 format.html { redirect_back_or_default _project_issues_path(@project) }
383 383 format.api { render_api_ok }
384 384 end
385 385 end
386 386
387 387 # Overrides Redmine::MenuManager::MenuController::ClassMethods for
388 388 # when the "New issue" tab is enabled
389 389 def current_menu_item
390 390 if Setting.new_item_menu_tab == '1' && [:new, :create].include?(action_name.to_sym)
391 391 :new_issue
392 392 else
393 393 super
394 394 end
395 395 end
396 396
397 397 private
398 398
399 399 def retrieve_previous_and_next_issue_ids
400 400 if params[:prev_issue_id].present? || params[:next_issue_id].present?
401 401 @prev_issue_id = params[:prev_issue_id].presence.try(:to_i)
402 402 @next_issue_id = params[:next_issue_id].presence.try(:to_i)
403 403 @issue_position = params[:issue_position].presence.try(:to_i)
404 404 @issue_count = params[:issue_count].presence.try(:to_i)
405 405 else
406 406 retrieve_query_from_session
407 407 if @query
408 408 sort_init(@query.sort_criteria.empty? ? [['id', 'desc']] : @query.sort_criteria)
409 409 sort_update(@query.sortable_columns, 'issues_index_sort')
410 410 limit = 500
411 411 issue_ids = @query.issue_ids(:order => sort_clause, :limit => (limit + 1), :include => [:assigned_to, :tracker, :priority, :category, :fixed_version])
412 412 if (idx = issue_ids.index(@issue.id)) && idx < limit
413 413 if issue_ids.size < 500
414 414 @issue_position = idx + 1
415 415 @issue_count = issue_ids.size
416 416 end
417 417 @prev_issue_id = issue_ids[idx - 1] if idx > 0
418 418 @next_issue_id = issue_ids[idx + 1] if idx < (issue_ids.size - 1)
419 419 end
420 420 end
421 421 end
422 422 end
423 423
424 424 def previous_and_next_issue_ids_params
425 425 {
426 426 :prev_issue_id => params[:prev_issue_id],
427 427 :next_issue_id => params[:next_issue_id],
428 428 :issue_position => params[:issue_position],
429 429 :issue_count => params[:issue_count]
430 430 }.reject {|k,v| k.blank?}
431 431 end
432 432
433 433 # Used by #edit and #update to set some common instance variables
434 434 # from the params
435 435 def update_issue_from_params
436 436 @time_entry = TimeEntry.new(:issue => @issue, :project => @issue.project)
437 437 if params[:time_entry]
438 438 @time_entry.safe_attributes = params[:time_entry]
439 439 end
440 440
441 441 @issue.init_journal(User.current)
442 442
443 443 issue_attributes = params[:issue]
444 444 if issue_attributes && params[:conflict_resolution]
445 445 case params[:conflict_resolution]
446 446 when 'overwrite'
447 447 issue_attributes = issue_attributes.dup
448 448 issue_attributes.delete(:lock_version)
449 449 when 'add_notes'
450 450 issue_attributes = issue_attributes.slice(:notes, :private_notes)
451 451 when 'cancel'
452 452 redirect_to issue_path(@issue)
453 453 return false
454 454 end
455 455 end
456 456 @issue.safe_attributes = issue_attributes
457 457 @priorities = IssuePriority.active
458 458 @allowed_statuses = @issue.new_statuses_allowed_to(User.current)
459 459 true
460 460 end
461 461
462 462 # Used by #new and #create to build a new issue from the params
463 463 # The new issue will be copied from an existing one if copy_from parameter is given
464 464 def build_new_issue_from_params
465 465 @issue = Issue.new
466 466 if params[:copy_from]
467 467 begin
468 468 @issue.init_journal(User.current)
469 469 @copy_from = Issue.visible.find(params[:copy_from])
470 470 unless User.current.allowed_to?(:copy_issues, @copy_from.project)
471 471 raise ::Unauthorized
472 472 end
473 473 @link_copy = link_copy?(params[:link_copy]) || request.get?
474 474 @copy_attachments = params[:copy_attachments].present? || request.get?
475 475 @copy_subtasks = params[:copy_subtasks].present? || request.get?
476 476 @issue.copy_from(@copy_from, :attachments => @copy_attachments, :subtasks => @copy_subtasks, :link => @link_copy)
477 477 @issue.parent_issue_id = @copy_from.parent_id
478 478 rescue ActiveRecord::RecordNotFound
479 479 render_404
480 480 return
481 481 end
482 482 end
483 483 @issue.project = @project
484 484 if request.get?
485 485 @issue.project ||= @issue.allowed_target_projects.first
486 486 end
487 487 @issue.author ||= User.current
488 488 @issue.start_date ||= User.current.today if Setting.default_issue_start_date_to_creation_date?
489 489
490 490 attrs = (params[:issue] || {}).deep_dup
491 491 if action_name == 'new' && params[:was_default_status] == attrs[:status_id]
492 492 attrs.delete(:status_id)
493 493 end
494 494 if action_name == 'new' && params[:form_update_triggered_by] == 'issue_project_id'
495 495 # Discard submitted version when changing the project on the issue form
496 496 # so we can use the default version for the new project
497 497 attrs.delete(:fixed_version_id)
498 498 end
499 499 @issue.safe_attributes = attrs
500 500
501 501 if @issue.project
502 502 @issue.tracker ||= @issue.allowed_target_trackers.first
503 503 if @issue.tracker.nil?
504 504 if @issue.project.trackers.any?
505 505 # None of the project trackers is allowed to the user
506 506 render_error :message => l(:error_no_tracker_allowed_for_new_issue_in_project), :status => 403
507 507 else
508 508 # Project has no trackers
509 509 render_error l(:error_no_tracker_in_project)
510 510 end
511 511 return false
512 512 end
513 513 if @issue.status.nil?
514 514 render_error l(:error_no_default_issue_status)
515 515 return false
516 516 end
517 517 elsif request.get?
518 518 render_error :message => l(:error_no_projects_with_tracker_allowed_for_new_issue), :status => 403
519 519 return false
520 520 end
521 521
522 522 @priorities = IssuePriority.active
523 523 @allowed_statuses = @issue.new_statuses_allowed_to(User.current)
524 524 end
525 525
526 526 # Saves @issue and a time_entry from the parameters
527 527 def save_issue_with_child_records
528 528 Issue.transaction do
529 529 if params[:time_entry] && (params[:time_entry][:hours].present? || params[:time_entry][:comments].present?) && User.current.allowed_to?(:log_time, @issue.project)
530 530 time_entry = @time_entry || TimeEntry.new
531 531 time_entry.project = @issue.project
532 532 time_entry.issue = @issue
533 533 time_entry.user = User.current
534 534 time_entry.spent_on = User.current.today
535 535 time_entry.attributes = params[:time_entry]
536 536 @issue.time_entries << time_entry
537 537 end
538 538
539 539 call_hook(:controller_issues_edit_before_save, { :params => params, :issue => @issue, :time_entry => time_entry, :journal => @issue.current_journal})
540 540 if @issue.save
541 541 call_hook(:controller_issues_edit_after_save, { :params => params, :issue => @issue, :time_entry => time_entry, :journal => @issue.current_journal})
542 542 else
543 543 raise ActiveRecord::Rollback
544 544 end
545 545 end
546 546 end
547 547
548 548 # Returns true if the issue copy should be linked
549 549 # to the original issue
550 550 def link_copy?(param)
551 551 case Setting.link_copied_issue
552 552 when 'yes'
553 553 true
554 554 when 'no'
555 555 false
556 556 when 'ask'
557 557 param == '1'
558 558 end
559 559 end
560 560
561 561 # Redirects user after a successful issue creation
562 562 def redirect_after_create
563 563 if params[:continue]
564 564 attrs = {:tracker_id => @issue.tracker, :parent_issue_id => @issue.parent_issue_id}.reject {|k,v| v.nil?}
565 565 if params[:project_id]
566 566 redirect_to new_project_issue_path(@issue.project, :issue => attrs)
567 567 else
568 568 attrs.merge! :project_id => @issue.project_id
569 569 redirect_to new_issue_path(:issue => attrs)
570 570 end
571 571 else
572 572 redirect_to issue_path(@issue)
573 573 end
574 574 end
575 575 end
General Comments 0
You need to be logged in to leave comments. Login now