comparison app/controllers/issues_controller.rb @ 511:107d36338b70 live

Merge from branch "cannam"
author Chris Cannam
date Thu, 14 Jul 2011 10:43:07 +0100
parents 851510f1b535
children 5e80956cc792
comparison
equal deleted inserted replaced
451:a9f6345cb43d 511:107d36338b70
1 # Redmine - project management software 1 # Redmine - project management software
2 # Copyright (C) 2006-2008 Jean-Philippe Lang 2 # Copyright (C) 2006-2011 Jean-Philippe Lang
3 # 3 #
4 # This program is free software; you can redistribute it and/or 4 # This program is free software; you can redistribute it and/or
5 # modify it under the terms of the GNU General Public License 5 # modify it under the terms of the GNU General Public License
6 # as published by the Free Software Foundation; either version 2 6 # as published by the Free Software Foundation; either version 2
7 # of the License, or (at your option) any later version. 7 # of the License, or (at your option) any later version.
8 # 8 #
9 # This program is distributed in the hope that it will be useful, 9 # This program is distributed in the hope that it will be useful,
10 # but WITHOUT ANY WARRANTY; without even the implied warranty of 10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 # GNU General Public License for more details. 12 # GNU General Public License for more details.
13 # 13 #
14 # You should have received a copy of the GNU General Public License 14 # You should have received a copy of the GNU General Public License
15 # along with this program; if not, write to the Free Software 15 # along with this program; if not, write to the Free Software
16 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. 16 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
17 17
18 class IssuesController < ApplicationController 18 class IssuesController < ApplicationController
19 menu_item :new_issue, :only => [:new, :create] 19 menu_item :new_issue, :only => [:new, :create]
20 default_search_scope :issues 20 default_search_scope :issues
21 21
22 before_filter :find_issue, :only => [:show, :edit, :update] 22 before_filter :find_issue, :only => [:show, :edit, :update]
23 before_filter :find_issues, :only => [:bulk_edit, :bulk_update, :move, :perform_move, :destroy] 23 before_filter :find_issues, :only => [:bulk_edit, :bulk_update, :move, :perform_move, :destroy]
24 before_filter :check_project_uniqueness, :only => [:move, :perform_move] 24 before_filter :check_project_uniqueness, :only => [:move, :perform_move]
25 before_filter :find_project, :only => [:new, :create] 25 before_filter :find_project, :only => [:new, :create]
26 before_filter :authorize, :except => [:index] 26 before_filter :authorize, :except => [:index]
27 before_filter :find_optional_project, :only => [:index] 27 before_filter :find_optional_project, :only => [:index]
28 before_filter :check_for_default_issue_status, :only => [:new, :create] 28 before_filter :check_for_default_issue_status, :only => [:new, :create]
29 before_filter :build_new_issue_from_params, :only => [:new, :create] 29 before_filter :build_new_issue_from_params, :only => [:new, :create]
30 accept_key_auth :index, :show, :create, :update, :destroy 30 accept_rss_auth :index, :show
31 accept_api_auth :index, :show, :create, :update, :destroy
31 32
32 rescue_from Query::StatementInvalid, :with => :query_statement_invalid 33 rescue_from Query::StatementInvalid, :with => :query_statement_invalid
33 34
34 helper :journals 35 helper :journals
35 helper :projects 36 helper :projects
36 include ProjectsHelper 37 include ProjectsHelper
37 helper :custom_fields 38 helper :custom_fields
38 include CustomFieldsHelper 39 include CustomFieldsHelper
39 helper :issue_relations 40 helper :issue_relations
40 include IssueRelationsHelper 41 include IssueRelationsHelper
41 helper :watchers 42 helper :watchers
42 include WatchersHelper 43 include WatchersHelper
43 helper :attachments 44 helper :attachments
44 include AttachmentsHelper 45 include AttachmentsHelper
45 helper :queries 46 helper :queries
46 include QueriesHelper 47 include QueriesHelper
48 helper :repositories
49 include RepositoriesHelper
47 helper :sort 50 helper :sort
48 include SortHelper 51 include SortHelper
49 include IssuesHelper 52 include IssuesHelper
50 helper :timelog 53 helper :timelog
51 helper :gantt 54 helper :gantt
56 :render => { :nothing => true, :status => :method_not_allowed } 59 :render => { :nothing => true, :status => :method_not_allowed }
57 60
58 verify :method => :post, :only => :create, :render => {:nothing => true, :status => :method_not_allowed } 61 verify :method => :post, :only => :create, :render => {:nothing => true, :status => :method_not_allowed }
59 verify :method => :post, :only => :bulk_update, :render => {:nothing => true, :status => :method_not_allowed } 62 verify :method => :post, :only => :bulk_update, :render => {:nothing => true, :status => :method_not_allowed }
60 verify :method => :put, :only => :update, :render => {:nothing => true, :status => :method_not_allowed } 63 verify :method => :put, :only => :update, :render => {:nothing => true, :status => :method_not_allowed }
61 64
62 def index 65 def index
63 retrieve_query 66 retrieve_query
64 sort_init(@query.sort_criteria.empty? ? [['id', 'desc']] : @query.sort_criteria) 67 sort_init(@query.sort_criteria.empty? ? [['id', 'desc']] : @query.sort_criteria)
65 sort_update(@query.sortable_columns) 68 sort_update(@query.sortable_columns)
66 69
67 if @query.valid? 70 if @query.valid?
68 limit = case params[:format] 71 case params[:format]
69 when 'csv', 'pdf' 72 when 'csv', 'pdf'
70 Setting.issues_export_limit.to_i 73 @limit = Setting.issues_export_limit.to_i
71 when 'atom' 74 when 'atom'
72 Setting.feeds_limit.to_i 75 @limit = Setting.feeds_limit.to_i
76 when 'xml', 'json'
77 @offset, @limit = api_offset_and_limit
73 else 78 else
74 per_page_option 79 @limit = per_page_option
75 end 80 end
76 81
77 @issue_count = @query.issue_count 82 @issue_count = @query.issue_count
78 @issue_pages = Paginator.new self, @issue_count, limit, params['page'] 83 @issue_pages = Paginator.new self, @issue_count, @limit, params['page']
84 @offset ||= @issue_pages.current.offset
79 @issues = @query.issues(:include => [:assigned_to, :tracker, :priority, :category, :fixed_version], 85 @issues = @query.issues(:include => [:assigned_to, :tracker, :priority, :category, :fixed_version],
80 :order => sort_clause, 86 :order => sort_clause,
81 :offset => @issue_pages.current.offset, 87 :offset => @offset,
82 :limit => limit) 88 :limit => @limit)
83 @issue_count_by_group = @query.issue_count_by_group 89 @issue_count_by_group = @query.issue_count_by_group
84 90
85 respond_to do |format| 91 respond_to do |format|
86 format.html { render :template => 'issues/index.rhtml', :layout => !request.xhr? } 92 format.html { render :template => 'issues/index.rhtml', :layout => !request.xhr? }
87 format.xml { render :layout => false } 93 format.api
88 format.json { render :text => @issues.to_json, :layout => false }
89 format.atom { render_feed(@issues, :title => "#{@project || Setting.app_title}: #{l(:label_issue_plural)}") } 94 format.atom { render_feed(@issues, :title => "#{@project || Setting.app_title}: #{l(:label_issue_plural)}") }
90 format.csv { send_data(issues_to_csv(@issues, @project), :type => 'text/csv; header=present', :filename => 'export.csv') } 95 format.csv { send_data(issues_to_csv(@issues, @project), :type => 'text/csv; header=present', :filename => 'export.csv') }
91 format.pdf { send_data(issues_to_pdf(@issues, @project, @query), :type => 'application/pdf', :filename => 'export.pdf') } 96 format.pdf { send_data(issues_to_pdf(@issues, @project, @query), :type => 'application/pdf', :filename => 'export.pdf') }
92 end 97 end
93 else 98 else
95 render(:template => 'issues/index.rhtml', :layout => !request.xhr?) 100 render(:template => 'issues/index.rhtml', :layout => !request.xhr?)
96 end 101 end
97 rescue ActiveRecord::RecordNotFound 102 rescue ActiveRecord::RecordNotFound
98 render_404 103 render_404
99 end 104 end
100 105
101 def show 106 def show
102 @journals = @issue.journals.find(:all, :include => [:user, :details], :order => "#{Journal.table_name}.created_on ASC") 107 @journals = @issue.journals.find(:all, :include => [:user, :details], :order => "#{Journal.table_name}.created_on ASC")
103 @journals.each_with_index {|j,i| j.indice = i+1} 108 @journals.each_with_index {|j,i| j.indice = i+1}
104 @journals.reverse! if User.current.wants_comments_in_reverse_order? 109 @journals.reverse! if User.current.wants_comments_in_reverse_order?
105 @changesets = @issue.changesets.visible.all 110
106 @changesets.reverse! if User.current.wants_comments_in_reverse_order? 111 if User.current.allowed_to?(:view_changesets, @project)
112 @changesets = @issue.changesets.visible.all
113 @changesets.reverse! if User.current.wants_comments_in_reverse_order?
114 end
115
116 @relations = @issue.relations.select {|r| r.other_issue(@issue) && r.other_issue(@issue).visible? }
107 @allowed_statuses = @issue.new_statuses_allowed_to(User.current) 117 @allowed_statuses = @issue.new_statuses_allowed_to(User.current)
108 @edit_allowed = User.current.allowed_to?(:edit_issues, @project) 118 @edit_allowed = User.current.allowed_to?(:edit_issues, @project)
109 @priorities = IssuePriority.all 119 @priorities = IssuePriority.all
110 @time_entry = TimeEntry.new 120 @time_entry = TimeEntry.new(:issue => @issue, :project => @issue.project)
111 respond_to do |format| 121 respond_to do |format|
112 format.html { render :template => 'issues/show.rhtml' } 122 format.html { render :template => 'issues/show.rhtml' }
113 format.xml { render :layout => false } 123 format.api
114 format.json { render :text => @issue.to_json, :layout => false }
115 format.atom { render :template => 'journals/index', :layout => false, :content_type => 'application/atom+xml' } 124 format.atom { render :template => 'journals/index', :layout => false, :content_type => 'application/atom+xml' }
116 format.pdf { send_data(issue_to_pdf(@issue), :type => 'application/pdf', :filename => "#{@project.identifier}-#{@issue.id}.pdf") } 125 format.pdf { send_data(issue_to_pdf(@issue), :type => 'application/pdf', :filename => "#{@project.identifier}-#{@issue.id}.pdf") }
117 end 126 end
118 end 127 end
119 128
145 respond_to do |format| 154 respond_to do |format|
146 format.html { 155 format.html {
147 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?} } : 156 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?} } :
148 { :action => 'show', :id => @issue }) 157 { :action => 'show', :id => @issue })
149 } 158 }
150 format.xml { render :action => 'show', :status => :created, :location => url_for(:controller => 'issues', :action => 'show', :id => @issue) } 159 format.api { render :action => 'show', :status => :created, :location => issue_url(@issue) }
151 format.json { render :text => @issue.to_json, :status => :created, :location => url_for(:controller => 'issues', :action => 'show'), :layout => false }
152 end 160 end
153 return 161 return
154 else 162 else
155 respond_to do |format| 163 respond_to do |format|
156 format.html { render :action => 'new' } 164 format.html { render :action => 'new' }
157 format.xml { render(:xml => @issue.errors, :status => :unprocessable_entity); return } 165 format.api { render_validation_errors(@issue) }
158 format.json { render :text => object_errors_to_json(@issue), :status => :unprocessable_entity, :layout => false } 166 end
159 end 167 end
160 end 168 end
161 end 169
162
163 def edit 170 def edit
164 update_issue_from_params 171 update_issue_from_params
165 172
166 @journal = @issue.current_journal 173 @journal = @issue.current_journal
167 174
178 render_attachment_warning_if_needed(@issue) 185 render_attachment_warning_if_needed(@issue)
179 flash[:notice] = l(:notice_successful_update) unless @issue.current_journal.new_record? 186 flash[:notice] = l(:notice_successful_update) unless @issue.current_journal.new_record?
180 187
181 respond_to do |format| 188 respond_to do |format|
182 format.html { redirect_back_or_default({:action => 'show', :id => @issue}) } 189 format.html { redirect_back_or_default({:action => 'show', :id => @issue}) }
183 format.xml { head :ok } 190 format.api { head :ok }
184 format.json { head :ok }
185 end 191 end
186 else 192 else
187 render_attachment_warning_if_needed(@issue) 193 render_attachment_warning_if_needed(@issue)
188 flash[:notice] = l(:notice_successful_update) unless @issue.current_journal.new_record? 194 flash[:notice] = l(:notice_successful_update) unless @issue.current_journal.new_record?
189 @journal = @issue.current_journal 195 @journal = @issue.current_journal
190 196
191 respond_to do |format| 197 respond_to do |format|
192 format.html { render :action => 'edit' } 198 format.html { render :action => 'edit' }
193 format.xml { render :xml => @issue.errors, :status => :unprocessable_entity } 199 format.api { render_validation_errors(@issue) }
194 format.json { render :text => object_errors_to_json(@issue), :status => :unprocessable_entity, :layout => false }
195 end 200 end
196 end 201 end
197 end 202 end
198 203
199 # Bulk edit a set of issues 204 # Bulk edit a set of issues
221 end 226 end
222 end 227 end
223 set_flash_from_bulk_issue_save(@issues, unsaved_issue_ids) 228 set_flash_from_bulk_issue_save(@issues, unsaved_issue_ids)
224 redirect_back_or_default({:controller => 'issues', :action => 'index', :project_id => @project}) 229 redirect_back_or_default({:controller => 'issues', :action => 'index', :project_id => @project})
225 end 230 end
226 231
227 def destroy 232 def destroy
228 @hours = TimeEntry.sum(:hours, :conditions => ['issue_id IN (?)', @issues]).to_f 233 @hours = TimeEntry.sum(:hours, :conditions => ['issue_id IN (?)', @issues]).to_f
229 if @hours > 0 234 if @hours > 0
230 case params[:todo] 235 case params[:todo]
231 when 'destroy' 236 when 'destroy'
239 return 244 return
240 else 245 else
241 TimeEntry.update_all("issue_id = #{reassign_to.id}", ['issue_id IN (?)', @issues]) 246 TimeEntry.update_all("issue_id = #{reassign_to.id}", ['issue_id IN (?)', @issues])
242 end 247 end
243 else 248 else
244 unless params[:format] == 'xml' || params[:format] == 'json' 249 # display the destroy form if it's a user request
245 # display the destroy form if it's a user request 250 return unless api_request?
246 return 251 end
247 end 252 end
248 end 253 @issues.each do |issue|
249 end 254 begin
250 @issues.each(&:destroy) 255 issue.reload.destroy
256 rescue ::ActiveRecord::RecordNotFound # raised by #reload if issue no longer exists
257 # nothing to do, issue was already deleted (eg. by a parent)
258 end
259 end
251 respond_to do |format| 260 respond_to do |format|
252 format.html { redirect_back_or_default(:action => 'index', :project_id => @project) } 261 format.html { redirect_back_or_default(:action => 'index', :project_id => @project) }
253 format.xml { head :ok } 262 format.api { head :ok }
254 format.json { head :ok }
255 end 263 end
256 end 264 end
257 265
258 private 266 private
259 def find_issue 267 def find_issue
268 # Issue.visible.find(...) can not be used to redirect user to the login form
269 # if the issue actually exists but requires authentication
260 @issue = Issue.find(params[:id], :include => [:project, :tracker, :status, :author, :priority, :category]) 270 @issue = Issue.find(params[:id], :include => [:project, :tracker, :status, :author, :priority, :category])
271 unless @issue.visible?
272 deny_access
273 return
274 end
261 @project = @issue.project 275 @project = @issue.project
262 rescue ActiveRecord::RecordNotFound 276 rescue ActiveRecord::RecordNotFound
263 render_404 277 render_404
264 end 278 end
265 279
266 def find_project 280 def find_project
267 project_id = (params[:issue] && params[:issue][:project_id]) || params[:project_id] 281 project_id = (params[:issue] && params[:issue][:project_id]) || params[:project_id]
268 @project = Project.find(project_id) 282 @project = Project.find(project_id)
269 rescue ActiveRecord::RecordNotFound 283 rescue ActiveRecord::RecordNotFound
270 render_404 284 render_404
271 end 285 end
272 286
273 # Used by #edit and #update to set some common instance variables 287 # Used by #edit and #update to set some common instance variables
274 # from the params 288 # from the params
275 # TODO: Refactor, not everything in here is needed by #edit 289 # TODO: Refactor, not everything in here is needed by #edit
276 def update_issue_from_params 290 def update_issue_from_params
277 @allowed_statuses = @issue.new_statuses_allowed_to(User.current) 291 @allowed_statuses = @issue.new_statuses_allowed_to(User.current)
278 @priorities = IssuePriority.all 292 @priorities = IssuePriority.all
279 @edit_allowed = User.current.allowed_to?(:edit_issues, @project) 293 @edit_allowed = User.current.allowed_to?(:edit_issues, @project)
280 @time_entry = TimeEntry.new 294 @time_entry = TimeEntry.new(:issue => @issue, :project => @issue.project)
281 @time_entry.attributes = params[:time_entry] 295 @time_entry.attributes = params[:time_entry]
282 296
283 @notes = params[:notes] || (params[:issue].present? ? params[:issue][:notes] : nil) 297 @notes = params[:notes] || (params[:issue].present? ? params[:issue][:notes] : nil)
284 @issue.init_journal(User.current, @notes) 298 @issue.init_journal(User.current, @notes)
285 @issue.safe_attributes = params[:issue] 299 @issue.safe_attributes = params[:issue]
286 300
287 # tests if the the user assigned_to_id 301 # tests if the the user assigned_to_id
288 # is in this issues watcher's list 302 # is in this issues watcher's list
289 # if not, adds it. 303 # if not, adds it.
290 304
291 if params[:issue][:assigned_to_id] && !params[:issue][:assigned_to_id].empty?: 305 if params[:issue] && params[:issue][:assigned_to_id] && !params[:issue][:assigned_to_id].empty?:
292 unless @issue.watched_by?(User.find(params[:issue][:assigned_to_id])): 306 unless @issue.watched_by?(User.find(params[:issue][:assigned_to_id])):
293 @issue.add_watcher(User.find(params[:issue][:assigned_to_id])) 307 @issue.add_watcher(User.find(params[:issue][:assigned_to_id]))
294 end 308 end
295 end 309 end
296 310
305 @issue.copy_from(params[:copy_from]) if params[:copy_from] 319 @issue.copy_from(params[:copy_from]) if params[:copy_from]
306 @issue.project = @project 320 @issue.project = @project
307 else 321 else
308 @issue = @project.issues.visible.find(params[:id]) 322 @issue = @project.issues.visible.find(params[:id])
309 end 323 end
310 324
311 @issue.project = @project 325 @issue.project = @project
326 @issue.author = User.current
312 # Tracker must be set before custom field values 327 # Tracker must be set before custom field values
313 @issue.tracker ||= @project.trackers.find((params[:issue] && params[:issue][:tracker_id]) || params[:tracker_id] || :first) 328 @issue.tracker ||= @project.trackers.find((params[:issue] && params[:issue][:tracker_id]) || params[:tracker_id] || :first)
314 if @issue.tracker.nil? 329 if @issue.tracker.nil?
315 render_error l(:error_no_tracker_in_project) 330 render_error l(:error_no_tracker_in_project)
316 return false 331 return false
320 @issue.safe_attributes = params[:issue] 335 @issue.safe_attributes = params[:issue]
321 if User.current.allowed_to?(:add_issue_watchers, @project) && @issue.new_record? 336 if User.current.allowed_to?(:add_issue_watchers, @project) && @issue.new_record?
322 @issue.watcher_user_ids = params[:issue]['watcher_user_ids'] 337 @issue.watcher_user_ids = params[:issue]['watcher_user_ids']
323 end 338 end
324 end 339 end
325 @issue.author = User.current
326 @priorities = IssuePriority.all 340 @priorities = IssuePriority.all
327 @allowed_statuses = @issue.new_statuses_allowed_to(User.current, true) 341 @allowed_statuses = @issue.new_statuses_allowed_to(User.current, true)
328 end 342 end
329 343
330 def check_for_default_issue_status 344 def check_for_default_issue_status