##// END OF EJS Templates
Merged r3947 from trunk....
Eric Davis -
r3883:d1a9f81fc6d1
parent child
Show More
@@ -1,367 +1,367
1 1 # Redmine - project management software
2 2 # Copyright (C) 2006-2008 Jean-Philippe Lang
3 3 #
4 4 # This program is free software; you can redistribute it and/or
5 5 # modify it under the terms of the GNU General Public License
6 6 # as published by the Free Software Foundation; either version 2
7 7 # of the License, or (at your option) any later version.
8 8 #
9 9 # This program is distributed in the hope that it will be useful,
10 10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
11 11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 12 # GNU General Public License for more details.
13 13 #
14 14 # You should have received a copy of the GNU General Public License
15 15 # along with this program; if not, write to the Free Software
16 16 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
17 17
18 18 class IssuesController < ApplicationController
19 19 menu_item :new_issue, :only => [:new, :create]
20 20 default_search_scope :issues
21 21
22 22 before_filter :find_issue, :only => [:show, :edit, :update]
23 23 before_filter :find_issues, :only => [:bulk_edit, :move, :perform_move, :destroy]
24 before_filter :find_project, :only => [:new, :create, :update_form, :preview]
25 before_filter :authorize, :except => [:index, :changes, :preview, :context_menu]
24 before_filter :find_project, :only => [:new, :create, :update_form]
25 before_filter :authorize, :except => [:index, :changes, :context_menu]
26 26 before_filter :find_optional_project, :only => [:index, :changes]
27 27 before_filter :check_for_default_issue_status, :only => [:new, :create]
28 28 before_filter :build_new_issue_from_params, :only => [:new, :create]
29 29 accept_key_auth :index, :show, :changes
30 30
31 31 rescue_from Query::StatementInvalid, :with => :query_statement_invalid
32 32
33 33 helper :journals
34 34 helper :projects
35 35 include ProjectsHelper
36 36 helper :custom_fields
37 37 include CustomFieldsHelper
38 38 helper :issue_relations
39 39 include IssueRelationsHelper
40 40 helper :watchers
41 41 include WatchersHelper
42 42 helper :attachments
43 43 include AttachmentsHelper
44 44 helper :queries
45 45 include QueriesHelper
46 46 helper :sort
47 47 include SortHelper
48 48 include IssuesHelper
49 49 helper :timelog
50 50 include Redmine::Export::PDF
51 51
52 52 verify :method => [:post, :delete],
53 53 :only => :destroy,
54 54 :render => { :nothing => true, :status => :method_not_allowed }
55 55
56 56 verify :method => :post, :only => :create, :render => {:nothing => true, :status => :method_not_allowed }
57 57 verify :method => :put, :only => :update, :render => {:nothing => true, :status => :method_not_allowed }
58 58
59 59 def index
60 60 retrieve_query
61 61 sort_init(@query.sort_criteria.empty? ? [['id', 'desc']] : @query.sort_criteria)
62 62 sort_update(@query.sortable_columns)
63 63
64 64 if @query.valid?
65 65 limit = case params[:format]
66 66 when 'csv', 'pdf'
67 67 Setting.issues_export_limit.to_i
68 68 when 'atom'
69 69 Setting.feeds_limit.to_i
70 70 else
71 71 per_page_option
72 72 end
73 73
74 74 @issue_count = @query.issue_count
75 75 @issue_pages = Paginator.new self, @issue_count, limit, params['page']
76 76 @issues = @query.issues(:include => [:assigned_to, :tracker, :priority, :category, :fixed_version],
77 77 :order => sort_clause,
78 78 :offset => @issue_pages.current.offset,
79 79 :limit => limit)
80 80 @issue_count_by_group = @query.issue_count_by_group
81 81
82 82 respond_to do |format|
83 83 format.html { render :template => 'issues/index.rhtml', :layout => !request.xhr? }
84 84 format.xml { render :layout => false }
85 85 format.json { render :text => @issues.to_json, :layout => false }
86 86 format.atom { render_feed(@issues, :title => "#{@project || Setting.app_title}: #{l(:label_issue_plural)}") }
87 87 format.csv { send_data(issues_to_csv(@issues, @project), :type => 'text/csv; header=present', :filename => 'export.csv') }
88 88 format.pdf { send_data(issues_to_pdf(@issues, @project, @query), :type => 'application/pdf', :filename => 'export.pdf') }
89 89 end
90 90 else
91 91 # Send html if the query is not valid
92 92 render(:template => 'issues/index.rhtml', :layout => !request.xhr?)
93 93 end
94 94 rescue ActiveRecord::RecordNotFound
95 95 render_404
96 96 end
97 97
98 98 def changes
99 99 retrieve_query
100 100 sort_init 'id', 'desc'
101 101 sort_update(@query.sortable_columns)
102 102
103 103 if @query.valid?
104 104 @journals = @query.journals(:order => "#{Journal.table_name}.created_on DESC",
105 105 :limit => 25)
106 106 end
107 107 @title = (@project ? @project.name : Setting.app_title) + ": " + (@query.new_record? ? l(:label_changes_details) : @query.name)
108 108 render :layout => false, :content_type => 'application/atom+xml'
109 109 rescue ActiveRecord::RecordNotFound
110 110 render_404
111 111 end
112 112
113 113 def show
114 114 @journals = @issue.journals.find(:all, :include => [:user, :details], :order => "#{Journal.table_name}.created_on ASC")
115 115 @journals.each_with_index {|j,i| j.indice = i+1}
116 116 @journals.reverse! if User.current.wants_comments_in_reverse_order?
117 117 @changesets = @issue.changesets.visible.all
118 118 @changesets.reverse! if User.current.wants_comments_in_reverse_order?
119 119 @allowed_statuses = @issue.new_statuses_allowed_to(User.current)
120 120 @edit_allowed = User.current.allowed_to?(:edit_issues, @project)
121 121 @priorities = IssuePriority.all
122 122 @time_entry = TimeEntry.new
123 123 respond_to do |format|
124 124 format.html { render :template => 'issues/show.rhtml' }
125 125 format.xml { render :layout => false }
126 126 format.json { render :text => @issue.to_json, :layout => false }
127 127 format.atom { render :action => 'changes', :layout => false, :content_type => 'application/atom+xml' }
128 128 format.pdf { send_data(issue_to_pdf(@issue), :type => 'application/pdf', :filename => "#{@project.identifier}-#{@issue.id}.pdf") }
129 129 end
130 130 end
131 131
132 132 # Add a new issue
133 133 # The new issue will be created from an existing one if copy_from parameter is given
134 134 def new
135 135 render :action => 'new', :layout => !request.xhr?
136 136 end
137 137
138 138 def create
139 139 call_hook(:controller_issues_new_before_save, { :params => params, :issue => @issue })
140 140 if @issue.save
141 141 attachments = Attachment.attach_files(@issue, params[:attachments])
142 142 render_attachment_warning_if_needed(@issue)
143 143 flash[:notice] = l(:notice_successful_create)
144 144 call_hook(:controller_issues_new_after_save, { :params => params, :issue => @issue})
145 145 respond_to do |format|
146 146 format.html {
147 147 redirect_to(params[:continue] ? { :action => 'new', :issue => {:tracker_id => @issue.tracker, :parent_issue_id => @issue.parent_issue_id}.reject {|k,v| v.nil?} } :
148 148 { :action => 'show', :id => @issue })
149 149 }
150 150 format.xml { render :action => 'show', :status => :created, :location => url_for(:controller => 'issues', :action => 'show', :id => @issue) }
151 151 format.json { render :text => @issue.to_json, :status => :created, :location => url_for(:controller => 'issues', :action => 'show'), :layout => false }
152 152 end
153 153 return
154 154 else
155 155 respond_to do |format|
156 156 format.html { render :action => 'new' }
157 157 format.xml { render(:xml => @issue.errors, :status => :unprocessable_entity); return }
158 158 format.json { render :text => object_errors_to_json(@issue), :status => :unprocessable_entity, :layout => false }
159 159 end
160 160 end
161 161 end
162 162
163 163 # Attributes that can be updated on workflow transition (without :edit permission)
164 164 # TODO: make it configurable (at least per role)
165 165 UPDATABLE_ATTRS_ON_TRANSITION = %w(status_id assigned_to_id fixed_version_id done_ratio) unless const_defined?(:UPDATABLE_ATTRS_ON_TRANSITION)
166 166
167 167 def edit
168 168 update_issue_from_params
169 169
170 170 @journal = @issue.current_journal
171 171
172 172 respond_to do |format|
173 173 format.html { }
174 174 format.xml { }
175 175 end
176 176 end
177 177
178 178 def update
179 179 update_issue_from_params
180 180
181 181 if @issue.save_issue_with_child_records(params, @time_entry)
182 182 render_attachment_warning_if_needed(@issue)
183 183 flash[:notice] = l(:notice_successful_update) unless @issue.current_journal.new_record?
184 184
185 185 respond_to do |format|
186 186 format.html { redirect_back_or_default({:action => 'show', :id => @issue}) }
187 187 format.xml { head :ok }
188 188 format.json { head :ok }
189 189 end
190 190 else
191 191 render_attachment_warning_if_needed(@issue)
192 192 flash[:notice] = l(:notice_successful_update) unless @issue.current_journal.new_record?
193 193 @journal = @issue.current_journal
194 194
195 195 respond_to do |format|
196 196 format.html { render :action => 'edit' }
197 197 format.xml { render :xml => @issue.errors, :status => :unprocessable_entity }
198 198 format.json { render :text => object_errors_to_json(@issue), :status => :unprocessable_entity, :layout => false }
199 199 end
200 200 end
201 201 end
202 202
203 203 # Bulk edit a set of issues
204 204 def bulk_edit
205 205 @issues.sort!
206 206 if request.post?
207 207 attributes = (params[:issue] || {}).reject {|k,v| v.blank?}
208 208 attributes.keys.each {|k| attributes[k] = '' if attributes[k] == 'none'}
209 209 attributes[:custom_field_values].reject! {|k,v| v.blank?} if attributes[:custom_field_values]
210 210
211 211 unsaved_issue_ids = []
212 212 @issues.each do |issue|
213 213 issue.reload
214 214 journal = issue.init_journal(User.current, params[:notes])
215 215 issue.safe_attributes = attributes
216 216 call_hook(:controller_issues_bulk_edit_before_save, { :params => params, :issue => issue })
217 217 unless issue.save
218 218 # Keep unsaved issue ids to display them in flash error
219 219 unsaved_issue_ids << issue.id
220 220 end
221 221 end
222 222 set_flash_from_bulk_issue_save(@issues, unsaved_issue_ids)
223 223 redirect_back_or_default({:controller => 'issues', :action => 'index', :project_id => @project})
224 224 return
225 225 end
226 226 @available_statuses = Workflow.available_statuses(@project)
227 227 @custom_fields = @project.all_issue_custom_fields
228 228 end
229 229
230 230 def destroy
231 231 @hours = TimeEntry.sum(:hours, :conditions => ['issue_id IN (?)', @issues]).to_f
232 232 if @hours > 0
233 233 case params[:todo]
234 234 when 'destroy'
235 235 # nothing to do
236 236 when 'nullify'
237 237 TimeEntry.update_all('issue_id = NULL', ['issue_id IN (?)', @issues])
238 238 when 'reassign'
239 239 reassign_to = @project.issues.find_by_id(params[:reassign_to_id])
240 240 if reassign_to.nil?
241 241 flash.now[:error] = l(:error_issue_not_found_in_project)
242 242 return
243 243 else
244 244 TimeEntry.update_all("issue_id = #{reassign_to.id}", ['issue_id IN (?)', @issues])
245 245 end
246 246 else
247 247 unless params[:format] == 'xml' || params[:format] == 'json'
248 248 # display the destroy form if it's a user request
249 249 return
250 250 end
251 251 end
252 252 end
253 253 @issues.each(&:destroy)
254 254 respond_to do |format|
255 255 format.html { redirect_to :action => 'index', :project_id => @project }
256 256 format.xml { head :ok }
257 257 format.json { head :ok }
258 258 end
259 259 end
260 260
261 261 def context_menu
262 262 @issues = Issue.find_all_by_id(params[:ids], :include => :project)
263 263 if (@issues.size == 1)
264 264 @issue = @issues.first
265 265 @allowed_statuses = @issue.new_statuses_allowed_to(User.current)
266 266 end
267 267 projects = @issues.collect(&:project).compact.uniq
268 268 @project = projects.first if projects.size == 1
269 269
270 270 @can = {:edit => (@project && User.current.allowed_to?(:edit_issues, @project)),
271 271 :log_time => (@project && User.current.allowed_to?(:log_time, @project)),
272 272 :update => (@project && (User.current.allowed_to?(:edit_issues, @project) || (User.current.allowed_to?(:change_status, @project) && @allowed_statuses && !@allowed_statuses.empty?))),
273 273 :move => (@project && User.current.allowed_to?(:move_issues, @project)),
274 274 :copy => (@issue && @project.trackers.include?(@issue.tracker) && User.current.allowed_to?(:add_issues, @project)),
275 275 :delete => (@project && User.current.allowed_to?(:delete_issues, @project))
276 276 }
277 277 if @project
278 278 @assignables = @project.assignable_users
279 279 @assignables << @issue.assigned_to if @issue && @issue.assigned_to && !@assignables.include?(@issue.assigned_to)
280 280 @trackers = @project.trackers
281 281 end
282 282
283 283 @priorities = IssuePriority.all.reverse
284 284 @statuses = IssueStatus.find(:all, :order => 'position')
285 285 @back = back_url
286 286
287 287 render :layout => false
288 288 end
289 289
290 290 def update_form
291 291 if params[:id].blank?
292 292 @issue = Issue.new
293 293 @issue.project = @project
294 294 else
295 295 @issue = @project.issues.visible.find(params[:id])
296 296 end
297 297 @issue.attributes = params[:issue]
298 298 @allowed_statuses = ([@issue.status] + @issue.status.find_new_statuses_allowed_to(User.current.roles_for_project(@project), @issue.tracker)).uniq
299 299 @priorities = IssuePriority.all
300 300
301 301 render :partial => 'attributes'
302 302 end
303 303
304 304 private
305 305 def find_issue
306 306 @issue = Issue.find(params[:id], :include => [:project, :tracker, :status, :author, :priority, :category])
307 307 @project = @issue.project
308 308 rescue ActiveRecord::RecordNotFound
309 309 render_404
310 310 end
311 311
312 312 def find_project
313 313 project_id = (params[:issue] && params[:issue][:project_id]) || params[:project_id]
314 314 @project = Project.find(project_id)
315 315 rescue ActiveRecord::RecordNotFound
316 316 render_404
317 317 end
318 318
319 319 # Used by #edit and #update to set some common instance variables
320 320 # from the params
321 321 # TODO: Refactor, not everything in here is needed by #edit
322 322 def update_issue_from_params
323 323 @allowed_statuses = @issue.new_statuses_allowed_to(User.current)
324 324 @priorities = IssuePriority.all
325 325 @edit_allowed = User.current.allowed_to?(:edit_issues, @project)
326 326 @time_entry = TimeEntry.new
327 327
328 328 @notes = params[:notes]
329 329 @issue.init_journal(User.current, @notes)
330 330 # User can change issue attributes only if he has :edit permission or if a workflow transition is allowed
331 331 if (@edit_allowed || !@allowed_statuses.empty?) && params[:issue]
332 332 attrs = params[:issue].dup
333 333 attrs.delete_if {|k,v| !UPDATABLE_ATTRS_ON_TRANSITION.include?(k) } unless @edit_allowed
334 334 attrs.delete(:status_id) unless @allowed_statuses.detect {|s| s.id.to_s == attrs[:status_id].to_s}
335 335 @issue.safe_attributes = attrs
336 336 end
337 337
338 338 end
339 339
340 340 # TODO: Refactor, lots of extra code in here
341 341 def build_new_issue_from_params
342 342 @issue = Issue.new
343 343 @issue.copy_from(params[:copy_from]) if params[:copy_from]
344 344 @issue.project = @project
345 345 # Tracker must be set before custom field values
346 346 @issue.tracker ||= @project.trackers.find((params[:issue] && params[:issue][:tracker_id]) || params[:tracker_id] || :first)
347 347 if @issue.tracker.nil?
348 348 render_error l(:error_no_tracker_in_project)
349 349 return false
350 350 end
351 351 if params[:issue].is_a?(Hash)
352 352 @issue.safe_attributes = params[:issue]
353 353 @issue.watcher_user_ids = params[:issue]['watcher_user_ids'] if User.current.allowed_to?(:add_issue_watchers, @project)
354 354 end
355 355 @issue.author = User.current
356 356 @issue.start_date ||= Date.today
357 357 @priorities = IssuePriority.all
358 358 @allowed_statuses = @issue.new_statuses_allowed_to(User.current, true)
359 359 end
360 360
361 361 def check_for_default_issue_status
362 362 if IssueStatus.default.nil?
363 363 render_error l(:error_no_default_issue_status)
364 364 return false
365 365 end
366 366 end
367 367 end
General Comments 0
You need to be logged in to leave comments. Login now