annotate app/controllers/issues_controller.rb @ 1459:cf78a7ade302 luisf

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