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