annotate .svn/pristine/f4/f440a7eb20f70f4e22fe7c8f34d5eed9370c603a.svn-base @ 1327:287f201c2802 redmine-2.2-integration

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