annotate .svn/pristine/7a/7a752fd775f30c709fd8d90851ab52b265da090b.svn-base @ 1295:622f24f53b42 redmine-2.3

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