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