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