annotate app/controllers/issues_controller.rb @ 1478:5ca1f4a47171 bibplugin_db_migrations

Close obsolete branch bibplugin_db_migrations
author Chris Cannam
date Fri, 30 Nov 2012 14:40:50 +0000
parents 5e80956cc792
children bb32da3bea34
rev   line source
Chris@0 1 # Redmine - project management software
Chris@441 2 # Copyright (C) 2006-2011 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@14 23 before_filter :find_issues, :only => [:bulk_edit, :bulk_update, :move, :perform_move, :destroy]
chris@37 24 before_filter :check_project_uniqueness, :only => [:move, :perform_move]
Chris@14 25 before_filter :find_project, :only => [:new, :create]
Chris@14 26 before_filter :authorize, :except => [:index]
Chris@14 27 before_filter :find_optional_project, :only => [:index]
Chris@0 28 before_filter :check_for_default_issue_status, :only => [:new, :create]
Chris@0 29 before_filter :build_new_issue_from_params, :only => [:new, :create]
Chris@507 30 accept_rss_auth :index, :show
Chris@507 31 accept_api_auth :index, :show, :create, :update, :destroy
Chris@0 32
Chris@0 33 rescue_from Query::StatementInvalid, :with => :query_statement_invalid
Chris@441 34
Chris@0 35 helper :journals
Chris@0 36 helper :projects
Chris@441 37 include ProjectsHelper
Chris@0 38 helper :custom_fields
Chris@0 39 include CustomFieldsHelper
Chris@0 40 helper :issue_relations
Chris@0 41 include IssueRelationsHelper
Chris@0 42 helper :watchers
Chris@0 43 include WatchersHelper
Chris@0 44 helper :attachments
Chris@0 45 include AttachmentsHelper
Chris@0 46 helper :queries
Chris@0 47 include QueriesHelper
Chris@117 48 helper :repositories
Chris@117 49 include RepositoriesHelper
Chris@0 50 helper :sort
Chris@0 51 include SortHelper
Chris@0 52 include IssuesHelper
Chris@0 53 helper :timelog
chris@22 54 helper :gantt
Chris@0 55 include Redmine::Export::PDF
Chris@0 56
Chris@0 57 verify :method => [:post, :delete],
Chris@0 58 :only => :destroy,
Chris@0 59 :render => { :nothing => true, :status => :method_not_allowed }
Chris@0 60
Chris@0 61 verify :method => :post, :only => :create, :render => {:nothing => true, :status => :method_not_allowed }
Chris@14 62 verify :method => :post, :only => :bulk_update, :render => {:nothing => true, :status => :method_not_allowed }
Chris@0 63 verify :method => :put, :only => :update, :render => {:nothing => true, :status => :method_not_allowed }
Chris@441 64
Chris@0 65 def index
Chris@0 66 retrieve_query
Chris@0 67 sort_init(@query.sort_criteria.empty? ? [['id', 'desc']] : @query.sort_criteria)
Chris@0 68 sort_update(@query.sortable_columns)
Chris@441 69
Chris@0 70 if @query.valid?
Chris@117 71 case params[:format]
Chris@0 72 when 'csv', 'pdf'
Chris@117 73 @limit = Setting.issues_export_limit.to_i
Chris@0 74 when 'atom'
Chris@117 75 @limit = Setting.feeds_limit.to_i
Chris@117 76 when 'xml', 'json'
Chris@117 77 @offset, @limit = api_offset_and_limit
Chris@0 78 else
Chris@117 79 @limit = per_page_option
Chris@0 80 end
Chris@441 81
Chris@0 82 @issue_count = @query.issue_count
Chris@117 83 @issue_pages = Paginator.new self, @issue_count, @limit, params['page']
Chris@117 84 @offset ||= @issue_pages.current.offset
Chris@0 85 @issues = @query.issues(:include => [:assigned_to, :tracker, :priority, :category, :fixed_version],
Chris@441 86 :order => sort_clause,
Chris@441 87 :offset => @offset,
Chris@117 88 :limit => @limit)
Chris@0 89 @issue_count_by_group = @query.issue_count_by_group
Chris@441 90
Chris@0 91 respond_to do |format|
Chris@909 92 format.html { render :template => 'issues/index', :layout => !request.xhr? }
Chris@909 93 format.api {
Chris@909 94 Issue.load_relations(@issues) if include_in_api_response?('relations')
Chris@909 95 }
Chris@0 96 format.atom { render_feed(@issues, :title => "#{@project || Setting.app_title}: #{l(:label_issue_plural)}") }
Chris@909 97 format.csv { send_data(issues_to_csv(@issues, @project, @query, params), :type => 'text/csv; header=present', :filename => 'export.csv') }
Chris@0 98 format.pdf { send_data(issues_to_pdf(@issues, @project, @query), :type => 'application/pdf', :filename => 'export.pdf') }
Chris@0 99 end
Chris@0 100 else
Chris@909 101 respond_to do |format|
Chris@909 102 format.html { render(:template => 'issues/index', :layout => !request.xhr?) }
Chris@909 103 format.any(:atom, :csv, :pdf) { render(:nothing => true) }
Chris@909 104 format.api { render_validation_errors(@query) }
Chris@909 105 end
Chris@0 106 end
Chris@0 107 rescue ActiveRecord::RecordNotFound
Chris@0 108 render_404
Chris@0 109 end
Chris@441 110
Chris@0 111 def show
Chris@0 112 @journals = @issue.journals.find(:all, :include => [:user, :details], :order => "#{Journal.table_name}.created_on ASC")
Chris@0 113 @journals.each_with_index {|j,i| j.indice = i+1}
Chris@0 114 @journals.reverse! if User.current.wants_comments_in_reverse_order?
Chris@441 115
Chris@441 116 if User.current.allowed_to?(:view_changesets, @project)
Chris@441 117 @changesets = @issue.changesets.visible.all
Chris@441 118 @changesets.reverse! if User.current.wants_comments_in_reverse_order?
Chris@441 119 end
Chris@441 120
Chris@210 121 @relations = @issue.relations.select {|r| r.other_issue(@issue) && r.other_issue(@issue).visible? }
Chris@0 122 @allowed_statuses = @issue.new_statuses_allowed_to(User.current)
Chris@0 123 @edit_allowed = User.current.allowed_to?(:edit_issues, @project)
Chris@909 124 @priorities = IssuePriority.active
Chris@441 125 @time_entry = TimeEntry.new(:issue => @issue, :project => @issue.project)
Chris@0 126 respond_to do |format|
Chris@909 127 format.html { render :template => 'issues/show' }
Chris@117 128 format.api
Chris@14 129 format.atom { render :template => 'journals/index', :layout => false, :content_type => 'application/atom+xml' }
Chris@0 130 format.pdf { send_data(issue_to_pdf(@issue), :type => 'application/pdf', :filename => "#{@project.identifier}-#{@issue.id}.pdf") }
Chris@0 131 end
Chris@0 132 end
Chris@0 133
Chris@0 134 # Add a new issue
Chris@0 135 # The new issue will be created from an existing one if copy_from parameter is given
Chris@0 136 def new
Chris@14 137 respond_to do |format|
Chris@14 138 format.html { render :action => 'new', :layout => !request.xhr? }
Chris@14 139 format.js { render :partial => 'attributes' }
Chris@14 140 end
Chris@0 141 end
Chris@0 142
Chris@0 143 def create
Chris@0 144 call_hook(:controller_issues_new_before_save, { :params => params, :issue => @issue })
Chris@0 145 if @issue.save
Chris@0 146 attachments = Attachment.attach_files(@issue, params[:attachments])
luisf@70 147
Chris@0 148 call_hook(:controller_issues_new_after_save, { :params => params, :issue => @issue})
luisf@70 149
luisf@71 150 # Also adds the assignee to the watcher's list
luisf@141 151 if params[:issue][:assigned_to_id] && !params[:issue][:assigned_to_id].empty?:
luisf@141 152 unless @issue.watcher_ids.include?(params[:issue][:assigned_to_id]):
luisf@141 153 @issue.add_watcher(User.find(params[:issue][:assigned_to_id]))
luisf@141 154 end
luisf@141 155 end
luisf@71 156
Chris@0 157 respond_to do |format|
Chris@0 158 format.html {
Chris@909 159 render_attachment_warning_if_needed(@issue)
Chris@909 160 flash[:notice] = l(:notice_issue_successful_create, :id => "<a href='#{issue_path(@issue)}'>##{@issue.id}</a>")
chris@22 161 redirect_to(params[:continue] ? { :action => 'new', :project_id => @project, :issue => {:tracker_id => @issue.tracker, :parent_issue_id => @issue.parent_issue_id}.reject {|k,v| v.nil?} } :
Chris@0 162 { :action => 'show', :id => @issue })
Chris@0 163 }
Chris@117 164 format.api { render :action => 'show', :status => :created, :location => issue_url(@issue) }
Chris@0 165 end
Chris@0 166 return
Chris@0 167 else
Chris@0 168 respond_to do |format|
Chris@0 169 format.html { render :action => 'new' }
Chris@117 170 format.api { render_validation_errors(@issue) }
Chris@0 171 end
Chris@0 172 end
Chris@0 173 end
Chris@441 174
Chris@0 175 def edit
Chris@0 176 update_issue_from_params
Chris@0 177
Chris@0 178 @journal = @issue.current_journal
Chris@0 179
Chris@0 180 respond_to do |format|
Chris@0 181 format.html { }
Chris@0 182 format.xml { }
Chris@0 183 end
Chris@0 184 end
Chris@0 185
Chris@0 186 def update
Chris@0 187 update_issue_from_params
Chris@0 188
Chris@0 189 if @issue.save_issue_with_child_records(params, @time_entry)
Chris@0 190 render_attachment_warning_if_needed(@issue)
Chris@0 191 flash[:notice] = l(:notice_successful_update) unless @issue.current_journal.new_record?
Chris@0 192
Chris@0 193 respond_to do |format|
Chris@0 194 format.html { redirect_back_or_default({:action => 'show', :id => @issue}) }
Chris@117 195 format.api { head :ok }
Chris@0 196 end
Chris@0 197 else
Chris@0 198 render_attachment_warning_if_needed(@issue)
Chris@0 199 flash[:notice] = l(:notice_successful_update) unless @issue.current_journal.new_record?
Chris@0 200 @journal = @issue.current_journal
Chris@0 201
Chris@0 202 respond_to do |format|
Chris@0 203 format.html { render :action => 'edit' }
Chris@117 204 format.api { render_validation_errors(@issue) }
Chris@0 205 end
Chris@0 206 end
Chris@0 207 end
Chris@0 208
Chris@0 209 # Bulk edit a set of issues
Chris@0 210 def bulk_edit
Chris@0 211 @issues.sort!
chris@37 212 @available_statuses = @projects.map{|p|Workflow.available_statuses(p)}.inject{|memo,w|memo & w}
chris@37 213 @custom_fields = @projects.map{|p|p.all_issue_custom_fields}.inject{|memo,c|memo & c}
chris@37 214 @assignables = @projects.map(&:assignable_users).inject{|memo,a| memo & a}
chris@37 215 @trackers = @projects.map(&:trackers).inject{|memo,t| memo & t}
Chris@0 216 end
Chris@0 217
Chris@14 218 def bulk_update
Chris@0 219 @issues.sort!
Chris@14 220 attributes = parse_params_for_bulk_issue_attributes(params)
Chris@14 221
Chris@14 222 unsaved_issue_ids = []
Chris@14 223 @issues.each do |issue|
Chris@14 224 issue.reload
Chris@14 225 journal = issue.init_journal(User.current, params[:notes])
Chris@14 226 issue.safe_attributes = attributes
Chris@14 227 call_hook(:controller_issues_bulk_edit_before_save, { :params => params, :issue => issue })
Chris@14 228 unless issue.save
Chris@14 229 # Keep unsaved issue ids to display them in flash error
Chris@14 230 unsaved_issue_ids << issue.id
Chris@0 231 end
Chris@0 232 end
Chris@14 233 set_flash_from_bulk_issue_save(@issues, unsaved_issue_ids)
Chris@14 234 redirect_back_or_default({:controller => 'issues', :action => 'index', :project_id => @project})
Chris@0 235 end
Chris@441 236
Chris@0 237 def destroy
Chris@0 238 @hours = TimeEntry.sum(:hours, :conditions => ['issue_id IN (?)', @issues]).to_f
Chris@0 239 if @hours > 0
Chris@0 240 case params[:todo]
Chris@0 241 when 'destroy'
Chris@0 242 # nothing to do
Chris@0 243 when 'nullify'
Chris@0 244 TimeEntry.update_all('issue_id = NULL', ['issue_id IN (?)', @issues])
Chris@0 245 when 'reassign'
Chris@0 246 reassign_to = @project.issues.find_by_id(params[:reassign_to_id])
Chris@0 247 if reassign_to.nil?
Chris@0 248 flash.now[:error] = l(:error_issue_not_found_in_project)
Chris@0 249 return
Chris@0 250 else
Chris@0 251 TimeEntry.update_all("issue_id = #{reassign_to.id}", ['issue_id IN (?)', @issues])
Chris@0 252 end
Chris@0 253 else
Chris@117 254 # display the destroy form if it's a user request
Chris@117 255 return unless api_request?
Chris@0 256 end
Chris@0 257 end
Chris@441 258 @issues.each do |issue|
Chris@441 259 begin
Chris@441 260 issue.reload.destroy
Chris@441 261 rescue ::ActiveRecord::RecordNotFound # raised by #reload if issue no longer exists
Chris@441 262 # nothing to do, issue was already deleted (eg. by a parent)
Chris@441 263 end
Chris@441 264 end
Chris@0 265 respond_to do |format|
chris@37 266 format.html { redirect_back_or_default(:action => 'index', :project_id => @project) }
Chris@117 267 format.api { head :ok }
Chris@0 268 end
Chris@0 269 end
Chris@0 270
Chris@0 271 private
Chris@0 272 def find_issue
Chris@441 273 # Issue.visible.find(...) can not be used to redirect user to the login form
Chris@441 274 # if the issue actually exists but requires authentication
Chris@0 275 @issue = Issue.find(params[:id], :include => [:project, :tracker, :status, :author, :priority, :category])
Chris@441 276 unless @issue.visible?
Chris@441 277 deny_access
Chris@441 278 return
Chris@441 279 end
Chris@0 280 @project = @issue.project
Chris@0 281 rescue ActiveRecord::RecordNotFound
Chris@0 282 render_404
Chris@0 283 end
Chris@441 284
Chris@0 285 def find_project
Chris@0 286 project_id = (params[:issue] && params[:issue][:project_id]) || params[:project_id]
Chris@0 287 @project = Project.find(project_id)
Chris@0 288 rescue ActiveRecord::RecordNotFound
Chris@0 289 render_404
Chris@0 290 end
Chris@441 291
Chris@0 292 # Used by #edit and #update to set some common instance variables
Chris@0 293 # from the params
Chris@0 294 # TODO: Refactor, not everything in here is needed by #edit
Chris@0 295 def update_issue_from_params
Chris@0 296 @allowed_statuses = @issue.new_statuses_allowed_to(User.current)
Chris@909 297 @priorities = IssuePriority.active
Chris@0 298 @edit_allowed = User.current.allowed_to?(:edit_issues, @project)
Chris@441 299 @time_entry = TimeEntry.new(:issue => @issue, :project => @issue.project)
chris@37 300 @time_entry.attributes = params[:time_entry]
Chris@441 301
chris@22 302 @notes = params[:notes] || (params[:issue].present? ? params[:issue][:notes] : nil)
Chris@0 303 @issue.init_journal(User.current, @notes)
chris@37 304 @issue.safe_attributes = params[:issue]
luisf@71 305
luisf@71 306 # tests if the the user assigned_to_id
luisf@71 307 # is in this issues watcher's list
luisf@71 308 # if not, adds it.
luisf@71 309
chris@496 310 if params[:issue] && params[:issue][:assigned_to_id] && !params[:issue][:assigned_to_id].empty?:
luisf@141 311 unless @issue.watched_by?(User.find(params[:issue][:assigned_to_id])):
luisf@141 312 @issue.add_watcher(User.find(params[:issue][:assigned_to_id]))
luisf@141 313 end
luisf@141 314 end
luisf@71 315
luisf@71 316
Chris@0 317 end
Chris@0 318
Chris@0 319 # TODO: Refactor, lots of extra code in here
chris@37 320 # TODO: Changing tracker on an existing issue should not trigger this
Chris@0 321 def build_new_issue_from_params
Chris@14 322 if params[:id].blank?
Chris@14 323 @issue = Issue.new
Chris@14 324 @issue.copy_from(params[:copy_from]) if params[:copy_from]
Chris@14 325 @issue.project = @project
Chris@14 326 else
Chris@14 327 @issue = @project.issues.visible.find(params[:id])
Chris@14 328 end
Chris@441 329
Chris@0 330 @issue.project = @project
Chris@507 331 @issue.author = User.current
Chris@0 332 # Tracker must be set before custom field values
Chris@0 333 @issue.tracker ||= @project.trackers.find((params[:issue] && params[:issue][:tracker_id]) || params[:tracker_id] || :first)
Chris@0 334 if @issue.tracker.nil?
Chris@0 335 render_error l(:error_no_tracker_in_project)
Chris@0 336 return false
Chris@0 337 end
Chris@909 338 @issue.start_date ||= Date.today if Setting.default_issue_start_date_to_creation_date?
Chris@0 339 if params[:issue].is_a?(Hash)
Chris@0 340 @issue.safe_attributes = params[:issue]
chris@37 341 if User.current.allowed_to?(:add_issue_watchers, @project) && @issue.new_record?
chris@37 342 @issue.watcher_user_ids = params[:issue]['watcher_user_ids']
chris@37 343 end
Chris@0 344 end
Chris@909 345 @priorities = IssuePriority.active
Chris@0 346 @allowed_statuses = @issue.new_statuses_allowed_to(User.current, true)
Chris@0 347 end
Chris@0 348
Chris@0 349 def check_for_default_issue_status
Chris@0 350 if IssueStatus.default.nil?
Chris@0 351 render_error l(:error_no_default_issue_status)
Chris@0 352 return false
Chris@0 353 end
Chris@0 354 end
Chris@14 355
Chris@14 356 def parse_params_for_bulk_issue_attributes(params)
Chris@14 357 attributes = (params[:issue] || {}).reject {|k,v| v.blank?}
Chris@14 358 attributes.keys.each {|k| attributes[k] = '' if attributes[k] == 'none'}
Chris@14 359 attributes[:custom_field_values].reject! {|k,v| v.blank?} if attributes[:custom_field_values]
Chris@14 360 attributes
Chris@14 361 end
Chris@0 362 end