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