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