##// END OF EJS Templates
Refactor: extract error to new method with before_filter....
Eric Davis -
r3576:dfc448030d17
parent child
Show More
@@ -1,584 +1,588
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
20 20 default_search_scope :issues
21 21
22 22 before_filter :find_issue, :only => [:show, :edit, :update, :reply]
23 23 before_filter :find_issues, :only => [:bulk_edit, :move, :destroy]
24 24 before_filter :find_project, :only => [:new, :create, :update_form, :preview, :auto_complete]
25 25 before_filter :authorize, :except => [:index, :changes, :gantt, :calendar, :preview, :context_menu]
26 26 before_filter :find_optional_project, :only => [:index, :changes, :gantt, :calendar]
27 before_filter :check_for_default_issue_status, :only => [:new, :create]
27 28 before_filter :build_new_issue_from_params, :only => [:new, :create]
28 29 accept_key_auth :index, :show, :changes
29 30
30 31 rescue_from Query::StatementInvalid, :with => :query_statement_invalid
31 32
32 33 helper :journals
33 34 helper :projects
34 35 include ProjectsHelper
35 36 helper :custom_fields
36 37 include CustomFieldsHelper
37 38 helper :issue_relations
38 39 include IssueRelationsHelper
39 40 helper :watchers
40 41 include WatchersHelper
41 42 helper :attachments
42 43 include AttachmentsHelper
43 44 helper :queries
44 45 include QueriesHelper
45 46 helper :sort
46 47 include SortHelper
47 48 include IssuesHelper
48 49 helper :timelog
49 50 include Redmine::Export::PDF
50 51
51 52 verify :method => [:post, :delete],
52 53 :only => :destroy,
53 54 :render => { :nothing => true, :status => :method_not_allowed }
54 55
55 56 verify :method => :post, :only => :create, :render => {:nothing => true, :status => :method_not_allowed }
56 57 verify :method => :put, :only => :update, :render => {:nothing => true, :status => :method_not_allowed }
57 58
58 59 def index
59 60 retrieve_query
60 61 sort_init(@query.sort_criteria.empty? ? [['id', 'desc']] : @query.sort_criteria)
61 62 sort_update(@query.sortable_columns)
62 63
63 64 if @query.valid?
64 65 limit = case params[:format]
65 66 when 'csv', 'pdf'
66 67 Setting.issues_export_limit.to_i
67 68 when 'atom'
68 69 Setting.feeds_limit.to_i
69 70 else
70 71 per_page_option
71 72 end
72 73
73 74 @issue_count = @query.issue_count
74 75 @issue_pages = Paginator.new self, @issue_count, limit, params['page']
75 76 @issues = @query.issues(:include => [:assigned_to, :tracker, :priority, :category, :fixed_version],
76 77 :order => sort_clause,
77 78 :offset => @issue_pages.current.offset,
78 79 :limit => limit)
79 80 @issue_count_by_group = @query.issue_count_by_group
80 81
81 82 respond_to do |format|
82 83 format.html { render :template => 'issues/index.rhtml', :layout => !request.xhr? }
83 84 format.xml { render :layout => false }
84 85 format.atom { render_feed(@issues, :title => "#{@project || Setting.app_title}: #{l(:label_issue_plural)}") }
85 86 format.csv { send_data(issues_to_csv(@issues, @project), :type => 'text/csv; header=present', :filename => 'export.csv') }
86 87 format.pdf { send_data(issues_to_pdf(@issues, @project, @query), :type => 'application/pdf', :filename => 'export.pdf') }
87 88 end
88 89 else
89 90 # Send html if the query is not valid
90 91 render(:template => 'issues/index.rhtml', :layout => !request.xhr?)
91 92 end
92 93 rescue ActiveRecord::RecordNotFound
93 94 render_404
94 95 end
95 96
96 97 def changes
97 98 retrieve_query
98 99 sort_init 'id', 'desc'
99 100 sort_update(@query.sortable_columns)
100 101
101 102 if @query.valid?
102 103 @journals = @query.journals(:order => "#{Journal.table_name}.created_on DESC",
103 104 :limit => 25)
104 105 end
105 106 @title = (@project ? @project.name : Setting.app_title) + ": " + (@query.new_record? ? l(:label_changes_details) : @query.name)
106 107 render :layout => false, :content_type => 'application/atom+xml'
107 108 rescue ActiveRecord::RecordNotFound
108 109 render_404
109 110 end
110 111
111 112 def show
112 113 @journals = @issue.journals.find(:all, :include => [:user, :details], :order => "#{Journal.table_name}.created_on ASC")
113 114 @journals.each_with_index {|j,i| j.indice = i+1}
114 115 @journals.reverse! if User.current.wants_comments_in_reverse_order?
115 116 @changesets = @issue.changesets.visible.all
116 117 @changesets.reverse! if User.current.wants_comments_in_reverse_order?
117 118 @allowed_statuses = @issue.new_statuses_allowed_to(User.current)
118 119 @edit_allowed = User.current.allowed_to?(:edit_issues, @project)
119 120 @priorities = IssuePriority.all
120 121 @time_entry = TimeEntry.new
121 122 respond_to do |format|
122 123 format.html { render :template => 'issues/show.rhtml' }
123 124 format.xml { render :layout => false }
124 125 format.atom { render :action => 'changes', :layout => false, :content_type => 'application/atom+xml' }
125 126 format.pdf { send_data(issue_to_pdf(@issue), :type => 'application/pdf', :filename => "#{@project.identifier}-#{@issue.id}.pdf") }
126 127 end
127 128 end
128 129
129 130 # Add a new issue
130 131 # The new issue will be created from an existing one if copy_from parameter is given
131 132 def new
132 133 render :action => 'new', :layout => !request.xhr?
133 134 end
134 135
135 136 def create
136 137 call_hook(:controller_issues_new_before_save, { :params => params, :issue => @issue })
137 138 if @issue.save
138 139 attachments = Attachment.attach_files(@issue, params[:attachments])
139 140 render_attachment_warning_if_needed(@issue)
140 141 flash[:notice] = l(:notice_successful_create)
141 142 call_hook(:controller_issues_new_after_save, { :params => params, :issue => @issue})
142 143 respond_to do |format|
143 144 format.html {
144 145 redirect_to(params[:continue] ? { :action => 'new', :issue => {:tracker_id => @issue.tracker, :parent_issue_id => @issue.parent_issue_id}.reject {|k,v| v.nil?} } :
145 146 { :action => 'show', :id => @issue })
146 147 }
147 148 format.xml { render :action => 'show', :status => :created, :location => url_for(:controller => 'issues', :action => 'show', :id => @issue) }
148 149 end
149 150 return
150 151 else
151 152 respond_to do |format|
152 153 format.html { render :action => 'new' }
153 154 format.xml { render(:xml => @issue.errors, :status => :unprocessable_entity); return }
154 155 end
155 156 end
156 157 end
157 158
158 159 # Attributes that can be updated on workflow transition (without :edit permission)
159 160 # TODO: make it configurable (at least per role)
160 161 UPDATABLE_ATTRS_ON_TRANSITION = %w(status_id assigned_to_id fixed_version_id done_ratio) unless const_defined?(:UPDATABLE_ATTRS_ON_TRANSITION)
161 162
162 163 def edit
163 164 update_issue_from_params
164 165
165 166 @journal = @issue.current_journal
166 167
167 168 respond_to do |format|
168 169 format.html { }
169 170 format.xml { }
170 171 end
171 172 end
172 173
173 174 def update
174 175 update_issue_from_params
175 176
176 177 if @issue.save_issue_with_child_records(params, @time_entry)
177 178 render_attachment_warning_if_needed(@issue)
178 179 flash[:notice] = l(:notice_successful_update) unless @issue.current_journal.new_record?
179 180
180 181 respond_to do |format|
181 182 format.html { redirect_back_or_default({:action => 'show', :id => @issue}) }
182 183 format.xml { head :ok }
183 184 end
184 185 else
185 186 render_attachment_warning_if_needed(@issue)
186 187 flash[:notice] = l(:notice_successful_update) unless @issue.current_journal.new_record?
187 188 @journal = @issue.current_journal
188 189
189 190 respond_to do |format|
190 191 format.html { render :action => 'edit' }
191 192 format.xml { render :xml => @issue.errors, :status => :unprocessable_entity }
192 193 end
193 194 end
194 195 end
195 196
196 197 def reply
197 198 journal = Journal.find(params[:journal_id]) if params[:journal_id]
198 199 if journal
199 200 user = journal.user
200 201 text = journal.notes
201 202 else
202 203 user = @issue.author
203 204 text = @issue.description
204 205 end
205 206 # Replaces pre blocks with [...]
206 207 text = text.to_s.strip.gsub(%r{<pre>((.|\s)*?)</pre>}m, '[...]')
207 208 content = "#{ll(Setting.default_language, :text_user_wrote, user)}\n> "
208 209 content << text.gsub(/(\r?\n|\r\n?)/, "\n> ") + "\n\n"
209 210
210 211 render(:update) { |page|
211 212 page.<< "$('notes').value = \"#{escape_javascript content}\";"
212 213 page.show 'update'
213 214 page << "Form.Element.focus('notes');"
214 215 page << "Element.scrollTo('update');"
215 216 page << "$('notes').scrollTop = $('notes').scrollHeight - $('notes').clientHeight;"
216 217 }
217 218 end
218 219
219 220 # Bulk edit a set of issues
220 221 def bulk_edit
221 222 @issues.sort!
222 223 if request.post?
223 224 attributes = (params[:issue] || {}).reject {|k,v| v.blank?}
224 225 attributes.keys.each {|k| attributes[k] = '' if attributes[k] == 'none'}
225 226 attributes[:custom_field_values].reject! {|k,v| v.blank?} if attributes[:custom_field_values]
226 227
227 228 unsaved_issue_ids = []
228 229 @issues.each do |issue|
229 230 issue.reload
230 231 journal = issue.init_journal(User.current, params[:notes])
231 232 issue.safe_attributes = attributes
232 233 call_hook(:controller_issues_bulk_edit_before_save, { :params => params, :issue => issue })
233 234 unless issue.save
234 235 # Keep unsaved issue ids to display them in flash error
235 236 unsaved_issue_ids << issue.id
236 237 end
237 238 end
238 239 set_flash_from_bulk_issue_save(@issues, unsaved_issue_ids)
239 240 redirect_back_or_default({:controller => 'issues', :action => 'index', :project_id => @project})
240 241 return
241 242 end
242 243 @available_statuses = Workflow.available_statuses(@project)
243 244 @custom_fields = @project.all_issue_custom_fields
244 245 end
245 246
246 247 def move
247 248 @issues.sort!
248 249 @copy = params[:copy_options] && params[:copy_options][:copy]
249 250 @allowed_projects = Issue.allowed_target_projects_on_move
250 251 @target_project = @allowed_projects.detect {|p| p.id.to_s == params[:new_project_id]} if params[:new_project_id]
251 252 @target_project ||= @project
252 253 @trackers = @target_project.trackers
253 254 @available_statuses = Workflow.available_statuses(@project)
254 255 if request.post?
255 256 new_tracker = params[:new_tracker_id].blank? ? nil : @target_project.trackers.find_by_id(params[:new_tracker_id])
256 257 unsaved_issue_ids = []
257 258 moved_issues = []
258 259 @issues.each do |issue|
259 260 issue.reload
260 261 changed_attributes = {}
261 262 [:assigned_to_id, :status_id, :start_date, :due_date].each do |valid_attribute|
262 263 unless params[valid_attribute].blank?
263 264 changed_attributes[valid_attribute] = (params[valid_attribute] == 'none' ? nil : params[valid_attribute])
264 265 end
265 266 end
266 267 issue.init_journal(User.current)
267 268 call_hook(:controller_issues_move_before_save, { :params => params, :issue => issue, :target_project => @target_project, :copy => !!@copy })
268 269 if r = issue.move_to_project(@target_project, new_tracker, {:copy => @copy, :attributes => changed_attributes})
269 270 moved_issues << r
270 271 else
271 272 unsaved_issue_ids << issue.id
272 273 end
273 274 end
274 275 set_flash_from_bulk_issue_save(@issues, unsaved_issue_ids)
275 276
276 277 if params[:follow]
277 278 if @issues.size == 1 && moved_issues.size == 1
278 279 redirect_to :controller => 'issues', :action => 'show', :id => moved_issues.first
279 280 else
280 281 redirect_to :controller => 'issues', :action => 'index', :project_id => (@target_project || @project)
281 282 end
282 283 else
283 284 redirect_to :controller => 'issues', :action => 'index', :project_id => @project
284 285 end
285 286 return
286 287 end
287 288 render :layout => false if request.xhr?
288 289 end
289 290
290 291 def destroy
291 292 @hours = TimeEntry.sum(:hours, :conditions => ['issue_id IN (?)', @issues]).to_f
292 293 if @hours > 0
293 294 case params[:todo]
294 295 when 'destroy'
295 296 # nothing to do
296 297 when 'nullify'
297 298 TimeEntry.update_all('issue_id = NULL', ['issue_id IN (?)', @issues])
298 299 when 'reassign'
299 300 reassign_to = @project.issues.find_by_id(params[:reassign_to_id])
300 301 if reassign_to.nil?
301 302 flash.now[:error] = l(:error_issue_not_found_in_project)
302 303 return
303 304 else
304 305 TimeEntry.update_all("issue_id = #{reassign_to.id}", ['issue_id IN (?)', @issues])
305 306 end
306 307 else
307 308 unless params[:format] == 'xml'
308 309 # display the destroy form if it's a user request
309 310 return
310 311 end
311 312 end
312 313 end
313 314 @issues.each(&:destroy)
314 315 respond_to do |format|
315 316 format.html { redirect_to :action => 'index', :project_id => @project }
316 317 format.xml { head :ok }
317 318 end
318 319 end
319 320
320 321 def gantt
321 322 @gantt = Redmine::Helpers::Gantt.new(params)
322 323 retrieve_query
323 324 @query.group_by = nil
324 325 if @query.valid?
325 326 events = []
326 327 # Issues that have start and due dates
327 328 events += @query.issues(:include => [:tracker, :assigned_to, :priority],
328 329 :order => "start_date, due_date",
329 330 :conditions => ["(((start_date>=? and start_date<=?) or (due_date>=? and due_date<=?) or (start_date<? and due_date>?)) and start_date is not null and due_date is not null)", @gantt.date_from, @gantt.date_to, @gantt.date_from, @gantt.date_to, @gantt.date_from, @gantt.date_to]
330 331 )
331 332 # Issues that don't have a due date but that are assigned to a version with a date
332 333 events += @query.issues(:include => [:tracker, :assigned_to, :priority, :fixed_version],
333 334 :order => "start_date, effective_date",
334 335 :conditions => ["(((start_date>=? and start_date<=?) or (effective_date>=? and effective_date<=?) or (start_date<? and effective_date>?)) and start_date is not null and due_date is null and effective_date is not null)", @gantt.date_from, @gantt.date_to, @gantt.date_from, @gantt.date_to, @gantt.date_from, @gantt.date_to]
335 336 )
336 337 # Versions
337 338 events += @query.versions(:conditions => ["effective_date BETWEEN ? AND ?", @gantt.date_from, @gantt.date_to])
338 339
339 340 @gantt.events = events
340 341 end
341 342
342 343 basename = (@project ? "#{@project.identifier}-" : '') + 'gantt'
343 344
344 345 respond_to do |format|
345 346 format.html { render :template => "issues/gantt.rhtml", :layout => !request.xhr? }
346 347 format.png { send_data(@gantt.to_image, :disposition => 'inline', :type => 'image/png', :filename => "#{basename}.png") } if @gantt.respond_to?('to_image')
347 348 format.pdf { send_data(gantt_to_pdf(@gantt, @project), :type => 'application/pdf', :filename => "#{basename}.pdf") }
348 349 end
349 350 end
350 351
351 352 def calendar
352 353 if params[:year] and params[:year].to_i > 1900
353 354 @year = params[:year].to_i
354 355 if params[:month] and params[:month].to_i > 0 and params[:month].to_i < 13
355 356 @month = params[:month].to_i
356 357 end
357 358 end
358 359 @year ||= Date.today.year
359 360 @month ||= Date.today.month
360 361
361 362 @calendar = Redmine::Helpers::Calendar.new(Date.civil(@year, @month, 1), current_language, :month)
362 363 retrieve_query
363 364 @query.group_by = nil
364 365 if @query.valid?
365 366 events = []
366 367 events += @query.issues(:include => [:tracker, :assigned_to, :priority],
367 368 :conditions => ["((start_date BETWEEN ? AND ?) OR (due_date BETWEEN ? AND ?))", @calendar.startdt, @calendar.enddt, @calendar.startdt, @calendar.enddt]
368 369 )
369 370 events += @query.versions(:conditions => ["effective_date BETWEEN ? AND ?", @calendar.startdt, @calendar.enddt])
370 371
371 372 @calendar.events = events
372 373 end
373 374
374 375 render :layout => false if request.xhr?
375 376 end
376 377
377 378 def context_menu
378 379 @issues = Issue.find_all_by_id(params[:ids], :include => :project)
379 380 if (@issues.size == 1)
380 381 @issue = @issues.first
381 382 @allowed_statuses = @issue.new_statuses_allowed_to(User.current)
382 383 end
383 384 projects = @issues.collect(&:project).compact.uniq
384 385 @project = projects.first if projects.size == 1
385 386
386 387 @can = {:edit => (@project && User.current.allowed_to?(:edit_issues, @project)),
387 388 :log_time => (@project && User.current.allowed_to?(:log_time, @project)),
388 389 :update => (@project && (User.current.allowed_to?(:edit_issues, @project) || (User.current.allowed_to?(:change_status, @project) && @allowed_statuses && !@allowed_statuses.empty?))),
389 390 :move => (@project && User.current.allowed_to?(:move_issues, @project)),
390 391 :copy => (@issue && @project.trackers.include?(@issue.tracker) && User.current.allowed_to?(:add_issues, @project)),
391 392 :delete => (@project && User.current.allowed_to?(:delete_issues, @project))
392 393 }
393 394 if @project
394 395 @assignables = @project.assignable_users
395 396 @assignables << @issue.assigned_to if @issue && @issue.assigned_to && !@assignables.include?(@issue.assigned_to)
396 397 @trackers = @project.trackers
397 398 end
398 399
399 400 @priorities = IssuePriority.all.reverse
400 401 @statuses = IssueStatus.find(:all, :order => 'position')
401 402 @back = params[:back_url] || request.env['HTTP_REFERER']
402 403
403 404 render :layout => false
404 405 end
405 406
406 407 def update_form
407 408 if params[:id].blank?
408 409 @issue = Issue.new
409 410 @issue.project = @project
410 411 else
411 412 @issue = @project.issues.visible.find(params[:id])
412 413 end
413 414 @issue.attributes = params[:issue]
414 415 @allowed_statuses = ([@issue.status] + @issue.status.find_new_statuses_allowed_to(User.current.roles_for_project(@project), @issue.tracker)).uniq
415 416 @priorities = IssuePriority.all
416 417
417 418 render :partial => 'attributes'
418 419 end
419 420
420 421 def preview
421 422 @issue = @project.issues.find_by_id(params[:id]) unless params[:id].blank?
422 423 if @issue
423 424 @attachements = @issue.attachments
424 425 @description = params[:issue] && params[:issue][:description]
425 426 if @description && @description.gsub(/(\r?\n|\n\r?)/, "\n") == @issue.description.to_s.gsub(/(\r?\n|\n\r?)/, "\n")
426 427 @description = nil
427 428 end
428 429 @notes = params[:notes]
429 430 else
430 431 @description = (params[:issue] ? params[:issue][:description] : nil)
431 432 end
432 433 render :layout => false
433 434 end
434 435
435 436 def auto_complete
436 437 @issues = []
437 438 q = params[:q].to_s
438 439 if q.match(/^\d+$/)
439 440 @issues << @project.issues.visible.find_by_id(q.to_i)
440 441 end
441 442 unless q.blank?
442 443 @issues += @project.issues.visible.find(:all, :conditions => ["LOWER(#{Issue.table_name}.subject) LIKE ?", "%#{q.downcase}%"], :limit => 10)
443 444 end
444 445 render :layout => false
445 446 end
446 447
447 448 private
448 449 def find_issue
449 450 @issue = Issue.find(params[:id], :include => [:project, :tracker, :status, :author, :priority, :category])
450 451 @project = @issue.project
451 452 rescue ActiveRecord::RecordNotFound
452 453 render_404
453 454 end
454 455
455 456 # Filter for bulk operations
456 457 def find_issues
457 458 @issues = Issue.find_all_by_id(params[:id] || params[:ids])
458 459 raise ActiveRecord::RecordNotFound if @issues.empty?
459 460 projects = @issues.collect(&:project).compact.uniq
460 461 if projects.size == 1
461 462 @project = projects.first
462 463 else
463 464 # TODO: let users bulk edit/move/destroy issues from different projects
464 465 render_error 'Can not bulk edit/move/destroy issues from different projects'
465 466 return false
466 467 end
467 468 rescue ActiveRecord::RecordNotFound
468 469 render_404
469 470 end
470 471
471 472 def find_project
472 473 project_id = (params[:issue] && params[:issue][:project_id]) || params[:project_id]
473 474 @project = Project.find(project_id)
474 475 rescue ActiveRecord::RecordNotFound
475 476 render_404
476 477 end
477 478
478 479 def find_optional_project
479 480 @project = Project.find(params[:project_id]) unless params[:project_id].blank?
480 481 allowed = User.current.allowed_to?({:controller => params[:controller], :action => params[:action]}, @project, :global => true)
481 482 allowed ? true : deny_access
482 483 rescue ActiveRecord::RecordNotFound
483 484 render_404
484 485 end
485 486
486 487 # Retrieve query from session or build a new query
487 488 def retrieve_query
488 489 if !params[:query_id].blank?
489 490 cond = "project_id IS NULL"
490 491 cond << " OR project_id = #{@project.id}" if @project
491 492 @query = Query.find(params[:query_id], :conditions => cond)
492 493 @query.project = @project
493 494 session[:query] = {:id => @query.id, :project_id => @query.project_id}
494 495 sort_clear
495 496 else
496 497 if api_request? || params[:set_filter] || session[:query].nil? || session[:query][:project_id] != (@project ? @project.id : nil)
497 498 # Give it a name, required to be valid
498 499 @query = Query.new(:name => "_")
499 500 @query.project = @project
500 501 if params[:fields] and params[:fields].is_a? Array
501 502 params[:fields].each do |field|
502 503 @query.add_filter(field, params[:operators][field], params[:values][field])
503 504 end
504 505 else
505 506 @query.available_filters.keys.each do |field|
506 507 @query.add_short_filter(field, params[field]) if params[field]
507 508 end
508 509 end
509 510 @query.group_by = params[:group_by]
510 511 @query.column_names = params[:query] && params[:query][:column_names]
511 512 session[:query] = {:project_id => @query.project_id, :filters => @query.filters, :group_by => @query.group_by, :column_names => @query.column_names}
512 513 else
513 514 @query = Query.find_by_id(session[:query][:id]) if session[:query][:id]
514 515 @query ||= Query.new(:name => "_", :project => @project, :filters => session[:query][:filters], :group_by => session[:query][:group_by], :column_names => session[:query][:column_names])
515 516 @query.project = @project
516 517 end
517 518 end
518 519 end
519 520
520 521 # Rescues an invalid query statement. Just in case...
521 522 def query_statement_invalid(exception)
522 523 logger.error "Query::StatementInvalid: #{exception.message}" if logger
523 524 session.delete(:query)
524 525 sort_clear
525 526 render_error "An error occurred while executing the query and has been logged. Please report this error to your Redmine administrator."
526 527 end
527 528
528 529 # Used by #edit and #update to set some common instance variables
529 530 # from the params
530 531 # TODO: Refactor, not everything in here is needed by #edit
531 532 def update_issue_from_params
532 533 @allowed_statuses = @issue.new_statuses_allowed_to(User.current)
533 534 @priorities = IssuePriority.all
534 535 @edit_allowed = User.current.allowed_to?(:edit_issues, @project)
535 536 @time_entry = TimeEntry.new
536 537
537 538 @notes = params[:notes]
538 539 @issue.init_journal(User.current, @notes)
539 540 # User can change issue attributes only if he has :edit permission or if a workflow transition is allowed
540 541 if (@edit_allowed || !@allowed_statuses.empty?) && params[:issue]
541 542 attrs = params[:issue].dup
542 543 attrs.delete_if {|k,v| !UPDATABLE_ATTRS_ON_TRANSITION.include?(k) } unless @edit_allowed
543 544 attrs.delete(:status_id) unless @allowed_statuses.detect {|s| s.id.to_s == attrs[:status_id].to_s}
544 545 @issue.safe_attributes = attrs
545 546 end
546 547
547 548 end
548 549
549 550 # TODO: Refactor, lots of extra code in here
550 551 def build_new_issue_from_params
551 552 @issue = Issue.new
552 553 @issue.copy_from(params[:copy_from]) if params[:copy_from]
553 554 @issue.project = @project
554 555 # Tracker must be set before custom field values
555 556 @issue.tracker ||= @project.trackers.find((params[:issue] && params[:issue][:tracker_id]) || params[:tracker_id] || :first)
556 557 if @issue.tracker.nil?
557 558 render_error l(:error_no_tracker_in_project)
558 559 return false
559 560 end
560 if @issue.status.nil?
561 render_error l(:error_no_default_issue_status)
562 return false
563 end
564 561 if params[:issue].is_a?(Hash)
565 562 @issue.safe_attributes = params[:issue]
566 563 @issue.watcher_user_ids = params[:issue]['watcher_user_ids'] if User.current.allowed_to?(:add_issue_watchers, @project)
567 564 end
568 565 @issue.author = User.current
569 566 @issue.start_date ||= Date.today
570 567 @priorities = IssuePriority.all
571 568 @allowed_statuses = @issue.new_statuses_allowed_to(User.current, true)
572 569 end
573 570
574 571 def set_flash_from_bulk_issue_save(issues, unsaved_issue_ids)
575 572 if unsaved_issue_ids.empty?
576 573 flash[:notice] = l(:notice_successful_update) unless issues.empty?
577 574 else
578 575 flash[:error] = l(:notice_failed_to_save_issues,
579 576 :count => unsaved_issue_ids.size,
580 577 :total => issues.size,
581 578 :ids => '#' + unsaved_issue_ids.join(', #'))
582 579 end
583 580 end
581
582 def check_for_default_issue_status
583 if IssueStatus.default.nil?
584 render_error l(:error_no_default_issue_status)
585 return false
586 end
587 end
584 588 end
General Comments 0
You need to be logged in to leave comments. Login now