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