comparison app/controllers/.svn/text-base/issues_controller.rb.svn-base @ 511:107d36338b70 live

Merge from branch "cannam"
author Chris Cannam
date Thu, 14 Jul 2011 10:43:07 +0100
parents 0c939c159af4
children
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
136 respond_to do |format| 145 respond_to do |format|
137 format.html { 146 format.html {
138 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?} } : 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?} } :
139 { :action => 'show', :id => @issue }) 148 { :action => 'show', :id => @issue })
140 } 149 }
141 format.xml { render :action => 'show', :status => :created, :location => url_for(:controller => 'issues', :action => 'show', :id => @issue) } 150 format.api { render :action => 'show', :status => :created, :location => issue_url(@issue) }
142 format.json { render :text => @issue.to_json, :status => :created, :location => url_for(:controller => 'issues', :action => 'show'), :layout => false }
143 end 151 end
144 return 152 return
145 else 153 else
146 respond_to do |format| 154 respond_to do |format|
147 format.html { render :action => 'new' } 155 format.html { render :action => 'new' }
148 format.xml { render(:xml => @issue.errors, :status => :unprocessable_entity); return } 156 format.api { render_validation_errors(@issue) }
149 format.json { render :text => object_errors_to_json(@issue), :status => :unprocessable_entity, :layout => false } 157 end
150 end 158 end
151 end 159 end
152 end 160
153
154 def edit 161 def edit
155 update_issue_from_params 162 update_issue_from_params
156 163
157 @journal = @issue.current_journal 164 @journal = @issue.current_journal
158 165
169 render_attachment_warning_if_needed(@issue) 176 render_attachment_warning_if_needed(@issue)
170 flash[:notice] = l(:notice_successful_update) unless @issue.current_journal.new_record? 177 flash[:notice] = l(:notice_successful_update) unless @issue.current_journal.new_record?
171 178
172 respond_to do |format| 179 respond_to do |format|
173 format.html { redirect_back_or_default({:action => 'show', :id => @issue}) } 180 format.html { redirect_back_or_default({:action => 'show', :id => @issue}) }
174 format.xml { head :ok } 181 format.api { head :ok }
175 format.json { head :ok }
176 end 182 end
177 else 183 else
178 render_attachment_warning_if_needed(@issue) 184 render_attachment_warning_if_needed(@issue)
179 flash[:notice] = l(:notice_successful_update) unless @issue.current_journal.new_record? 185 flash[:notice] = l(:notice_successful_update) unless @issue.current_journal.new_record?
180 @journal = @issue.current_journal 186 @journal = @issue.current_journal
181 187
182 respond_to do |format| 188 respond_to do |format|
183 format.html { render :action => 'edit' } 189 format.html { render :action => 'edit' }
184 format.xml { render :xml => @issue.errors, :status => :unprocessable_entity } 190 format.api { render_validation_errors(@issue) }
185 format.json { render :text => object_errors_to_json(@issue), :status => :unprocessable_entity, :layout => false }
186 end 191 end
187 end 192 end
188 end 193 end
189 194
190 # Bulk edit a set of issues 195 # Bulk edit a set of issues
212 end 217 end
213 end 218 end
214 set_flash_from_bulk_issue_save(@issues, unsaved_issue_ids) 219 set_flash_from_bulk_issue_save(@issues, unsaved_issue_ids)
215 redirect_back_or_default({:controller => 'issues', :action => 'index', :project_id => @project}) 220 redirect_back_or_default({:controller => 'issues', :action => 'index', :project_id => @project})
216 end 221 end
217 222
218 def destroy 223 def destroy
219 @hours = TimeEntry.sum(:hours, :conditions => ['issue_id IN (?)', @issues]).to_f 224 @hours = TimeEntry.sum(:hours, :conditions => ['issue_id IN (?)', @issues]).to_f
220 if @hours > 0 225 if @hours > 0
221 case params[:todo] 226 case params[:todo]
222 when 'destroy' 227 when 'destroy'
230 return 235 return
231 else 236 else
232 TimeEntry.update_all("issue_id = #{reassign_to.id}", ['issue_id IN (?)', @issues]) 237 TimeEntry.update_all("issue_id = #{reassign_to.id}", ['issue_id IN (?)', @issues])
233 end 238 end
234 else 239 else
235 unless params[:format] == 'xml' || params[:format] == 'json' 240 # display the destroy form if it's a user request
236 # display the destroy form if it's a user request 241 return unless api_request?
237 return 242 end
238 end 243 end
239 end 244 @issues.each do |issue|
240 end 245 begin
241 @issues.each(&:destroy) 246 issue.reload.destroy
247 rescue ::ActiveRecord::RecordNotFound # raised by #reload if issue no longer exists
248 # nothing to do, issue was already deleted (eg. by a parent)
249 end
250 end
242 respond_to do |format| 251 respond_to do |format|
243 format.html { redirect_back_or_default(:action => 'index', :project_id => @project) } 252 format.html { redirect_back_or_default(:action => 'index', :project_id => @project) }
244 format.xml { head :ok } 253 format.api { head :ok }
245 format.json { head :ok }
246 end 254 end
247 end 255 end
248 256
249 private 257 private
250 def find_issue 258 def find_issue
259 # Issue.visible.find(...) can not be used to redirect user to the login form
260 # if the issue actually exists but requires authentication
251 @issue = Issue.find(params[:id], :include => [:project, :tracker, :status, :author, :priority, :category]) 261 @issue = Issue.find(params[:id], :include => [:project, :tracker, :status, :author, :priority, :category])
262 unless @issue.visible?
263 deny_access
264 return
265 end
252 @project = @issue.project 266 @project = @issue.project
253 rescue ActiveRecord::RecordNotFound 267 rescue ActiveRecord::RecordNotFound
254 render_404 268 render_404
255 end 269 end
256 270
257 def find_project 271 def find_project
258 project_id = (params[:issue] && params[:issue][:project_id]) || params[:project_id] 272 project_id = (params[:issue] && params[:issue][:project_id]) || params[:project_id]
259 @project = Project.find(project_id) 273 @project = Project.find(project_id)
260 rescue ActiveRecord::RecordNotFound 274 rescue ActiveRecord::RecordNotFound
261 render_404 275 render_404
262 end 276 end
263 277
264 # Used by #edit and #update to set some common instance variables 278 # Used by #edit and #update to set some common instance variables
265 # from the params 279 # from the params
266 # TODO: Refactor, not everything in here is needed by #edit 280 # TODO: Refactor, not everything in here is needed by #edit
267 def update_issue_from_params 281 def update_issue_from_params
268 @allowed_statuses = @issue.new_statuses_allowed_to(User.current) 282 @allowed_statuses = @issue.new_statuses_allowed_to(User.current)
269 @priorities = IssuePriority.all 283 @priorities = IssuePriority.all
270 @edit_allowed = User.current.allowed_to?(:edit_issues, @project) 284 @edit_allowed = User.current.allowed_to?(:edit_issues, @project)
271 @time_entry = TimeEntry.new 285 @time_entry = TimeEntry.new(:issue => @issue, :project => @issue.project)
272 @time_entry.attributes = params[:time_entry] 286 @time_entry.attributes = params[:time_entry]
273 287
274 @notes = params[:notes] || (params[:issue].present? ? params[:issue][:notes] : nil) 288 @notes = params[:notes] || (params[:issue].present? ? params[:issue][:notes] : nil)
275 @issue.init_journal(User.current, @notes) 289 @issue.init_journal(User.current, @notes)
276 @issue.safe_attributes = params[:issue] 290 @issue.safe_attributes = params[:issue]
277 end 291 end
278 292
284 @issue.copy_from(params[:copy_from]) if params[:copy_from] 298 @issue.copy_from(params[:copy_from]) if params[:copy_from]
285 @issue.project = @project 299 @issue.project = @project
286 else 300 else
287 @issue = @project.issues.visible.find(params[:id]) 301 @issue = @project.issues.visible.find(params[:id])
288 end 302 end
289 303
290 @issue.project = @project 304 @issue.project = @project
305 @issue.author = User.current
291 # Tracker must be set before custom field values 306 # Tracker must be set before custom field values
292 @issue.tracker ||= @project.trackers.find((params[:issue] && params[:issue][:tracker_id]) || params[:tracker_id] || :first) 307 @issue.tracker ||= @project.trackers.find((params[:issue] && params[:issue][:tracker_id]) || params[:tracker_id] || :first)
293 if @issue.tracker.nil? 308 if @issue.tracker.nil?
294 render_error l(:error_no_tracker_in_project) 309 render_error l(:error_no_tracker_in_project)
295 return false 310 return false
299 @issue.safe_attributes = params[:issue] 314 @issue.safe_attributes = params[:issue]
300 if User.current.allowed_to?(:add_issue_watchers, @project) && @issue.new_record? 315 if User.current.allowed_to?(:add_issue_watchers, @project) && @issue.new_record?
301 @issue.watcher_user_ids = params[:issue]['watcher_user_ids'] 316 @issue.watcher_user_ids = params[:issue]['watcher_user_ids']
302 end 317 end
303 end 318 end
304 @issue.author = User.current
305 @priorities = IssuePriority.all 319 @priorities = IssuePriority.all
306 @allowed_statuses = @issue.new_statuses_allowed_to(User.current, true) 320 @allowed_statuses = @issue.new_statuses_allowed_to(User.current, true)
307 end 321 end
308 322
309 def check_for_default_issue_status 323 def check_for_default_issue_status