annotate app/controllers/issues_controller.rb @ 905:a97ce6b60fde yuya

Close obsolete branch yuya
author Chris Cannam
date Sat, 18 Sep 2010 17:38:00 +0100
parents 1d32c0a0efbf
children 40f7cfd4df19
rev   line source
Chris@0 1 # Redmine - project management software
Chris@0 2 # Copyright (C) 2006-2008 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@0 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@0 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@0 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@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@14 29 accept_key_auth :index, :show
Chris@0 30
Chris@0 31 rescue_from Query::StatementInvalid, :with => :query_statement_invalid
Chris@0 32
Chris@0 33 helper :journals
Chris@0 34 helper :projects
Chris@0 35 include ProjectsHelper
Chris@0 36 helper :custom_fields
Chris@0 37 include CustomFieldsHelper
Chris@0 38 helper :issue_relations
Chris@0 39 include IssueRelationsHelper
Chris@0 40 helper :watchers
Chris@0 41 include WatchersHelper
Chris@0 42 helper :attachments
Chris@0 43 include AttachmentsHelper
Chris@0 44 helper :queries
Chris@0 45 include QueriesHelper
Chris@0 46 helper :sort
Chris@0 47 include SortHelper
Chris@0 48 include IssuesHelper
Chris@0 49 helper :timelog
Chris@0 50 include Redmine::Export::PDF
Chris@0 51
Chris@0 52 verify :method => [:post, :delete],
Chris@0 53 :only => :destroy,
Chris@0 54 :render => { :nothing => true, :status => :method_not_allowed }
Chris@0 55
Chris@0 56 verify :method => :post, :only => :create, :render => {:nothing => true, :status => :method_not_allowed }
Chris@14 57 verify :method => :post, :only => :bulk_update, :render => {:nothing => true, :status => :method_not_allowed }
Chris@0 58 verify :method => :put, :only => :update, :render => {:nothing => true, :status => :method_not_allowed }
Chris@0 59
Chris@0 60 def index
Chris@0 61 retrieve_query
Chris@0 62 sort_init(@query.sort_criteria.empty? ? [['id', 'desc']] : @query.sort_criteria)
Chris@0 63 sort_update(@query.sortable_columns)
Chris@0 64
Chris@0 65 if @query.valid?
Chris@0 66 limit = case params[:format]
Chris@0 67 when 'csv', 'pdf'
Chris@0 68 Setting.issues_export_limit.to_i
Chris@0 69 when 'atom'
Chris@0 70 Setting.feeds_limit.to_i
Chris@0 71 else
Chris@0 72 per_page_option
Chris@0 73 end
Chris@0 74
Chris@0 75 @issue_count = @query.issue_count
Chris@0 76 @issue_pages = Paginator.new self, @issue_count, limit, params['page']
Chris@0 77 @issues = @query.issues(:include => [:assigned_to, :tracker, :priority, :category, :fixed_version],
Chris@0 78 :order => sort_clause,
Chris@0 79 :offset => @issue_pages.current.offset,
Chris@0 80 :limit => limit)
Chris@0 81 @issue_count_by_group = @query.issue_count_by_group
Chris@0 82
Chris@0 83 respond_to do |format|
Chris@0 84 format.html { render :template => 'issues/index.rhtml', :layout => !request.xhr? }
Chris@0 85 format.xml { render :layout => false }
Chris@0 86 format.json { render :text => @issues.to_json, :layout => false }
Chris@0 87 format.atom { render_feed(@issues, :title => "#{@project || Setting.app_title}: #{l(:label_issue_plural)}") }
Chris@0 88 format.csv { send_data(issues_to_csv(@issues, @project), :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@0 92 # Send html if the query is not valid
Chris@0 93 render(:template => 'issues/index.rhtml', :layout => !request.xhr?)
Chris@0 94 end
Chris@0 95 rescue ActiveRecord::RecordNotFound
Chris@0 96 render_404
Chris@0 97 end
Chris@0 98
Chris@0 99 def show
Chris@0 100 @journals = @issue.journals.find(:all, :include => [:user, :details], :order => "#{Journal.table_name}.created_on ASC")
Chris@0 101 @journals.each_with_index {|j,i| j.indice = i+1}
Chris@0 102 @journals.reverse! if User.current.wants_comments_in_reverse_order?
Chris@0 103 @changesets = @issue.changesets.visible.all
Chris@0 104 @changesets.reverse! if User.current.wants_comments_in_reverse_order?
Chris@0 105 @allowed_statuses = @issue.new_statuses_allowed_to(User.current)
Chris@0 106 @edit_allowed = User.current.allowed_to?(:edit_issues, @project)
Chris@0 107 @priorities = IssuePriority.all
Chris@0 108 @time_entry = TimeEntry.new
Chris@0 109 respond_to do |format|
Chris@0 110 format.html { render :template => 'issues/show.rhtml' }
Chris@0 111 format.xml { render :layout => false }
Chris@0 112 format.json { render :text => @issue.to_json, :layout => false }
Chris@14 113 format.atom { render :template => 'journals/index', :layout => false, :content_type => 'application/atom+xml' }
Chris@0 114 format.pdf { send_data(issue_to_pdf(@issue), :type => 'application/pdf', :filename => "#{@project.identifier}-#{@issue.id}.pdf") }
Chris@0 115 end
Chris@0 116 end
Chris@0 117
Chris@0 118 # Add a new issue
Chris@0 119 # The new issue will be created from an existing one if copy_from parameter is given
Chris@0 120 def new
Chris@14 121 respond_to do |format|
Chris@14 122 format.html { render :action => 'new', :layout => !request.xhr? }
Chris@14 123 format.js { render :partial => 'attributes' }
Chris@14 124 end
Chris@0 125 end
Chris@0 126
Chris@0 127 def create
Chris@0 128 call_hook(:controller_issues_new_before_save, { :params => params, :issue => @issue })
Chris@0 129 if @issue.save
Chris@0 130 attachments = Attachment.attach_files(@issue, params[:attachments])
Chris@0 131 render_attachment_warning_if_needed(@issue)
Chris@0 132 flash[:notice] = l(:notice_successful_create)
Chris@0 133 call_hook(:controller_issues_new_after_save, { :params => params, :issue => @issue})
Chris@0 134 respond_to do |format|
Chris@0 135 format.html {
Chris@0 136 redirect_to(params[:continue] ? { :action => 'new', :issue => {:tracker_id => @issue.tracker, :parent_issue_id => @issue.parent_issue_id}.reject {|k,v| v.nil?} } :
Chris@0 137 { :action => 'show', :id => @issue })
Chris@0 138 }
Chris@0 139 format.xml { render :action => 'show', :status => :created, :location => url_for(:controller => 'issues', :action => 'show', :id => @issue) }
Chris@0 140 format.json { render :text => @issue.to_json, :status => :created, :location => url_for(:controller => 'issues', :action => 'show'), :layout => false }
Chris@0 141 end
Chris@0 142 return
Chris@0 143 else
Chris@0 144 respond_to do |format|
Chris@0 145 format.html { render :action => 'new' }
Chris@0 146 format.xml { render(:xml => @issue.errors, :status => :unprocessable_entity); return }
Chris@0 147 format.json { render :text => object_errors_to_json(@issue), :status => :unprocessable_entity, :layout => false }
Chris@0 148 end
Chris@0 149 end
Chris@0 150 end
Chris@0 151
Chris@0 152 # Attributes that can be updated on workflow transition (without :edit permission)
Chris@0 153 # TODO: make it configurable (at least per role)
Chris@0 154 UPDATABLE_ATTRS_ON_TRANSITION = %w(status_id assigned_to_id fixed_version_id done_ratio) unless const_defined?(:UPDATABLE_ATTRS_ON_TRANSITION)
Chris@0 155
Chris@0 156 def edit
Chris@0 157 update_issue_from_params
Chris@0 158
Chris@0 159 @journal = @issue.current_journal
Chris@0 160
Chris@0 161 respond_to do |format|
Chris@0 162 format.html { }
Chris@0 163 format.xml { }
Chris@0 164 end
Chris@0 165 end
Chris@0 166
Chris@0 167 def update
Chris@0 168 update_issue_from_params
Chris@0 169
Chris@0 170 if @issue.save_issue_with_child_records(params, @time_entry)
Chris@0 171 render_attachment_warning_if_needed(@issue)
Chris@0 172 flash[:notice] = l(:notice_successful_update) unless @issue.current_journal.new_record?
Chris@0 173
Chris@0 174 respond_to do |format|
Chris@0 175 format.html { redirect_back_or_default({:action => 'show', :id => @issue}) }
Chris@0 176 format.xml { head :ok }
Chris@0 177 format.json { head :ok }
Chris@0 178 end
Chris@0 179 else
Chris@0 180 render_attachment_warning_if_needed(@issue)
Chris@0 181 flash[:notice] = l(:notice_successful_update) unless @issue.current_journal.new_record?
Chris@0 182 @journal = @issue.current_journal
Chris@0 183
Chris@0 184 respond_to do |format|
Chris@0 185 format.html { render :action => 'edit' }
Chris@0 186 format.xml { render :xml => @issue.errors, :status => :unprocessable_entity }
Chris@0 187 format.json { render :text => object_errors_to_json(@issue), :status => :unprocessable_entity, :layout => false }
Chris@0 188 end
Chris@0 189 end
Chris@0 190 end
Chris@0 191
Chris@0 192 # Bulk edit a set of issues
Chris@0 193 def bulk_edit
Chris@0 194 @issues.sort!
Chris@0 195 @available_statuses = Workflow.available_statuses(@project)
Chris@0 196 @custom_fields = @project.all_issue_custom_fields
Chris@0 197 end
Chris@0 198
Chris@14 199 def bulk_update
Chris@0 200 @issues.sort!
Chris@14 201 attributes = parse_params_for_bulk_issue_attributes(params)
Chris@14 202
Chris@14 203 unsaved_issue_ids = []
Chris@14 204 @issues.each do |issue|
Chris@14 205 issue.reload
Chris@14 206 journal = issue.init_journal(User.current, params[:notes])
Chris@14 207 issue.safe_attributes = attributes
Chris@14 208 call_hook(:controller_issues_bulk_edit_before_save, { :params => params, :issue => issue })
Chris@14 209 unless issue.save
Chris@14 210 # Keep unsaved issue ids to display them in flash error
Chris@14 211 unsaved_issue_ids << issue.id
Chris@0 212 end
Chris@0 213 end
Chris@14 214 set_flash_from_bulk_issue_save(@issues, unsaved_issue_ids)
Chris@14 215 redirect_back_or_default({:controller => 'issues', :action => 'index', :project_id => @project})
Chris@0 216 end
Chris@0 217
Chris@0 218 def destroy
Chris@0 219 @hours = TimeEntry.sum(:hours, :conditions => ['issue_id IN (?)', @issues]).to_f
Chris@0 220 if @hours > 0
Chris@0 221 case params[:todo]
Chris@0 222 when 'destroy'
Chris@0 223 # nothing to do
Chris@0 224 when 'nullify'
Chris@0 225 TimeEntry.update_all('issue_id = NULL', ['issue_id IN (?)', @issues])
Chris@0 226 when 'reassign'
Chris@0 227 reassign_to = @project.issues.find_by_id(params[:reassign_to_id])
Chris@0 228 if reassign_to.nil?
Chris@0 229 flash.now[:error] = l(:error_issue_not_found_in_project)
Chris@0 230 return
Chris@0 231 else
Chris@0 232 TimeEntry.update_all("issue_id = #{reassign_to.id}", ['issue_id IN (?)', @issues])
Chris@0 233 end
Chris@0 234 else
Chris@0 235 unless params[:format] == 'xml' || params[:format] == 'json'
Chris@0 236 # display the destroy form if it's a user request
Chris@0 237 return
Chris@0 238 end
Chris@0 239 end
Chris@0 240 end
Chris@0 241 @issues.each(&:destroy)
Chris@0 242 respond_to do |format|
Chris@0 243 format.html { redirect_to :action => 'index', :project_id => @project }
Chris@0 244 format.xml { head :ok }
Chris@0 245 format.json { head :ok }
Chris@0 246 end
Chris@0 247 end
Chris@0 248
Chris@0 249 private
Chris@0 250 def find_issue
Chris@0 251 @issue = Issue.find(params[:id], :include => [:project, :tracker, :status, :author, :priority, :category])
Chris@0 252 @project = @issue.project
Chris@0 253 rescue ActiveRecord::RecordNotFound
Chris@0 254 render_404
Chris@0 255 end
Chris@0 256
Chris@0 257 def find_project
Chris@0 258 project_id = (params[:issue] && params[:issue][:project_id]) || params[:project_id]
Chris@0 259 @project = Project.find(project_id)
Chris@0 260 rescue ActiveRecord::RecordNotFound
Chris@0 261 render_404
Chris@0 262 end
Chris@0 263
Chris@0 264 # Used by #edit and #update to set some common instance variables
Chris@0 265 # from the params
Chris@0 266 # TODO: Refactor, not everything in here is needed by #edit
Chris@0 267 def update_issue_from_params
Chris@0 268 @allowed_statuses = @issue.new_statuses_allowed_to(User.current)
Chris@0 269 @priorities = IssuePriority.all
Chris@0 270 @edit_allowed = User.current.allowed_to?(:edit_issues, @project)
Chris@0 271 @time_entry = TimeEntry.new
Chris@0 272
Chris@0 273 @notes = params[:notes]
Chris@0 274 @issue.init_journal(User.current, @notes)
Chris@0 275 # User can change issue attributes only if he has :edit permission or if a workflow transition is allowed
Chris@0 276 if (@edit_allowed || !@allowed_statuses.empty?) && params[:issue]
Chris@0 277 attrs = params[:issue].dup
Chris@0 278 attrs.delete_if {|k,v| !UPDATABLE_ATTRS_ON_TRANSITION.include?(k) } unless @edit_allowed
Chris@0 279 attrs.delete(:status_id) unless @allowed_statuses.detect {|s| s.id.to_s == attrs[:status_id].to_s}
Chris@0 280 @issue.safe_attributes = attrs
Chris@0 281 end
Chris@0 282
Chris@0 283 end
Chris@0 284
Chris@0 285 # TODO: Refactor, lots of extra code in here
Chris@0 286 def build_new_issue_from_params
Chris@14 287 if params[:id].blank?
Chris@14 288 @issue = Issue.new
Chris@14 289 @issue.copy_from(params[:copy_from]) if params[:copy_from]
Chris@14 290 @issue.project = @project
Chris@14 291 else
Chris@14 292 @issue = @project.issues.visible.find(params[:id])
Chris@14 293 end
Chris@14 294
Chris@0 295 @issue.project = @project
Chris@0 296 # Tracker must be set before custom field values
Chris@0 297 @issue.tracker ||= @project.trackers.find((params[:issue] && params[:issue][:tracker_id]) || params[:tracker_id] || :first)
Chris@0 298 if @issue.tracker.nil?
Chris@0 299 render_error l(:error_no_tracker_in_project)
Chris@0 300 return false
Chris@0 301 end
Chris@0 302 if params[:issue].is_a?(Hash)
Chris@0 303 @issue.safe_attributes = params[:issue]
Chris@0 304 @issue.watcher_user_ids = params[:issue]['watcher_user_ids'] if User.current.allowed_to?(:add_issue_watchers, @project)
Chris@0 305 end
Chris@0 306 @issue.author = User.current
Chris@0 307 @issue.start_date ||= Date.today
Chris@0 308 @priorities = IssuePriority.all
Chris@0 309 @allowed_statuses = @issue.new_statuses_allowed_to(User.current, true)
Chris@0 310 end
Chris@0 311
Chris@0 312 def check_for_default_issue_status
Chris@0 313 if IssueStatus.default.nil?
Chris@0 314 render_error l(:error_no_default_issue_status)
Chris@0 315 return false
Chris@0 316 end
Chris@0 317 end
Chris@14 318
Chris@14 319 def parse_params_for_bulk_issue_attributes(params)
Chris@14 320 attributes = (params[:issue] || {}).reject {|k,v| v.blank?}
Chris@14 321 attributes.keys.each {|k| attributes[k] = '' if attributes[k] == 'none'}
Chris@14 322 attributes[:custom_field_values].reject! {|k,v| v.blank?} if attributes[:custom_field_values]
Chris@14 323 attributes
Chris@14 324 end
Chris@0 325 end