@@ -1,410 +1,410 | |||
|
1 | 1 | # redMine - project management software |
|
2 | 2 | # Copyright (C) 2006-2007 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 | layout 'base' |
|
20 | 20 | menu_item :new_issue, :only => :new |
|
21 | 21 | |
|
22 | 22 | before_filter :find_issue, :only => [:show, :edit, :destroy_attachment] |
|
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, :preview, :update_form, :context_menu] |
|
26 | 26 | before_filter :find_optional_project, :only => [:index, :changes] |
|
27 | 27 | accept_key_auth :index, :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 :ifpdf |
|
35 | 35 | include IfpdfHelper |
|
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 | |
|
47 | 47 | def index |
|
48 | 48 | sort_init "#{Issue.table_name}.id", "desc" |
|
49 | 49 | sort_update |
|
50 | 50 | retrieve_query |
|
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 => sort_clause, |
|
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 { render :template => 'issues/index.rhtml', :layout => !request.xhr? } |
|
68 | 68 | format.atom { render_feed(@issues, :title => l(:label_issue_plural)) } |
|
69 | 69 | format.csv { send_data(issues_to_csv(@issues, @project).read, :type => 'text/csv; header=present', :filename => 'export.csv') } |
|
70 | 70 | format.pdf { send_data(render(:template => 'issues/index.rfpdf', :layout => false), :type => 'application/pdf', :filename => 'export.pdf') } |
|
71 | 71 | end |
|
72 | 72 | else |
|
73 | 73 | # Send html if the query is not valid |
|
74 | 74 | render(:template => 'issues/index.rhtml', :layout => !request.xhr?) |
|
75 | 75 | end |
|
76 | 76 | end |
|
77 | 77 | |
|
78 | 78 | def changes |
|
79 | 79 | sort_init "#{Issue.table_name}.id", "desc" |
|
80 | 80 | sort_update |
|
81 | 81 | retrieve_query |
|
82 | 82 | if @query.valid? |
|
83 | 83 | @journals = Journal.find :all, :include => [ :details, :user, {:issue => [:project, :author, :tracker, :status]} ], |
|
84 | 84 | :conditions => @query.statement, |
|
85 | 85 | :limit => 25, |
|
86 | 86 | :order => "#{Journal.table_name}.created_on DESC" |
|
87 | 87 | end |
|
88 | 88 | @title = (@project ? @project.name : Setting.app_title) + ": " + (@query.new_record? ? l(:label_changes_details) : @query.name) |
|
89 | 89 | render :layout => false, :content_type => 'application/atom+xml' |
|
90 | 90 | end |
|
91 | 91 | |
|
92 | 92 | def show |
|
93 | 93 | @custom_values = @project.custom_fields_for_issues(@issue.tracker).collect { |x| @issue.custom_values.find_by_custom_field_id(x.id) || CustomValue.new(:custom_field => x, :customized => @issue) } |
|
94 | 94 | @journals = @issue.journals.find(:all, :include => [:user, :details], :order => "#{Journal.table_name}.created_on ASC") |
|
95 | 95 | @journals.each_with_index {|j,i| j.indice = i+1} |
|
96 | 96 | @journals.reverse! if User.current.wants_comments_in_reverse_order? |
|
97 | 97 | @allowed_statuses = @issue.new_statuses_allowed_to(User.current) |
|
98 | 98 | @edit_allowed = User.current.allowed_to?(:edit_issues, @project) |
|
99 | 99 | @activities = Enumeration::get_values('ACTI') |
|
100 | 100 | @priorities = Enumeration::get_values('IPRI') |
|
101 | 101 | respond_to do |format| |
|
102 | 102 | format.html { render :template => 'issues/show.rhtml' } |
|
103 | 103 | format.atom { render :action => 'changes', :layout => false, :content_type => 'application/atom+xml' } |
|
104 | 104 | format.pdf { send_data(render(:template => 'issues/show.rfpdf', :layout => false), :type => 'application/pdf', :filename => "#{@project.identifier}-#{@issue.id}.pdf") } |
|
105 | 105 | end |
|
106 | 106 | end |
|
107 | 107 | |
|
108 | 108 | # Add a new issue |
|
109 | 109 | # The new issue will be created from an existing one if copy_from parameter is given |
|
110 | 110 | def new |
|
111 | 111 | @issue = params[:copy_from] ? Issue.new.copy_from(params[:copy_from]) : Issue.new(params[:issue]) |
|
112 | 112 | @issue.project = @project |
|
113 | 113 | @issue.author = User.current |
|
114 | 114 | @issue.tracker ||= @project.trackers.find(params[:tracker_id] ? params[:tracker_id] : :first) |
|
115 | 115 | if @issue.tracker.nil? |
|
116 | 116 | flash.now[:error] = 'No tracker is associated to this project. Please check the Project settings.' |
|
117 | 117 | render :nothing => true, :layout => true |
|
118 | 118 | return |
|
119 | 119 | end |
|
120 | 120 | |
|
121 | 121 | default_status = IssueStatus.default |
|
122 | 122 | unless default_status |
|
123 | 123 | flash.now[:error] = 'No default issue status is defined. Please check your configuration (Go to "Administration -> Issue statuses").' |
|
124 | 124 | render :nothing => true, :layout => true |
|
125 | 125 | return |
|
126 | 126 | end |
|
127 | 127 | @issue.status = default_status |
|
128 | 128 | @allowed_statuses = ([default_status] + default_status.find_new_statuses_allowed_to(User.current.role_for_project(@project), @issue.tracker)).uniq |
|
129 | 129 | |
|
130 | 130 | if request.get? || request.xhr? |
|
131 | 131 | @issue.start_date ||= Date.today |
|
132 | 132 | @custom_values = @issue.custom_values.empty? ? |
|
133 | 133 | @project.custom_fields_for_issues(@issue.tracker).collect { |x| CustomValue.new(:custom_field => x, :customized => @issue) } : |
|
134 | 134 | @issue.custom_values |
|
135 | 135 | else |
|
136 | 136 | requested_status = IssueStatus.find_by_id(params[:issue][:status_id]) |
|
137 | 137 | # Check that the user is allowed to apply the requested status |
|
138 | 138 | @issue.status = (@allowed_statuses.include? requested_status) ? requested_status : default_status |
|
139 | 139 | @custom_values = @project.custom_fields_for_issues(@issue.tracker).collect { |x| CustomValue.new(:custom_field => x, :customized => @issue, :value => params["custom_fields"][x.id.to_s]) } |
|
140 | 140 | @issue.custom_values = @custom_values |
|
141 | 141 | if @issue.save |
|
142 | 142 | attach_files(@issue, params[:attachments]) |
|
143 | 143 | flash[:notice] = l(:notice_successful_create) |
|
144 | 144 | Mailer.deliver_issue_add(@issue) if Setting.notified_events.include?('issue_added') |
|
145 |
redirect_to :controller => 'issues', :action => ' |
|
|
145 | redirect_to :controller => 'issues', :action => 'show', :id => @issue, :project_id => @project | |
|
146 | 146 | return |
|
147 | 147 | end |
|
148 | 148 | end |
|
149 | 149 | @priorities = Enumeration::get_values('IPRI') |
|
150 | 150 | render :layout => !request.xhr? |
|
151 | 151 | end |
|
152 | 152 | |
|
153 | 153 | # Attributes that can be updated on workflow transition (without :edit permission) |
|
154 | 154 | # TODO: make it configurable (at least per role) |
|
155 | 155 | UPDATABLE_ATTRS_ON_TRANSITION = %w(status_id assigned_to_id fixed_version_id done_ratio) unless const_defined?(:UPDATABLE_ATTRS_ON_TRANSITION) |
|
156 | 156 | |
|
157 | 157 | def edit |
|
158 | 158 | @allowed_statuses = @issue.new_statuses_allowed_to(User.current) |
|
159 | 159 | @activities = Enumeration::get_values('ACTI') |
|
160 | 160 | @priorities = Enumeration::get_values('IPRI') |
|
161 | 161 | @custom_values = [] |
|
162 | 162 | @edit_allowed = User.current.allowed_to?(:edit_issues, @project) |
|
163 | 163 | |
|
164 | 164 | @notes = params[:notes] |
|
165 | 165 | journal = @issue.init_journal(User.current, @notes) |
|
166 | 166 | # User can change issue attributes only if he has :edit permission or if a workflow transition is allowed |
|
167 | 167 | if (@edit_allowed || !@allowed_statuses.empty?) && params[:issue] |
|
168 | 168 | attrs = params[:issue].dup |
|
169 | 169 | attrs.delete_if {|k,v| !UPDATABLE_ATTRS_ON_TRANSITION.include?(k) } unless @edit_allowed |
|
170 | 170 | attrs.delete(:status_id) unless @allowed_statuses.detect {|s| s.id.to_s == attrs[:status_id].to_s} |
|
171 | 171 | @issue.attributes = attrs |
|
172 | 172 | end |
|
173 | 173 | |
|
174 | 174 | if request.get? |
|
175 | 175 | @custom_values = @project.custom_fields_for_issues(@issue.tracker).collect { |x| @issue.custom_values.find_by_custom_field_id(x.id) || CustomValue.new(:custom_field => x, :customized => @issue) } |
|
176 | 176 | else |
|
177 | 177 | # Update custom fields if user has :edit permission |
|
178 | 178 | if @edit_allowed && params[:custom_fields] |
|
179 | 179 | @custom_values = @project.custom_fields_for_issues(@issue.tracker).collect { |x| CustomValue.new(:custom_field => x, :customized => @issue, :value => params["custom_fields"][x.id.to_s]) } |
|
180 | 180 | @issue.custom_values = @custom_values |
|
181 | 181 | end |
|
182 | 182 | attachments = attach_files(@issue, params[:attachments]) |
|
183 | 183 | attachments.each {|a| journal.details << JournalDetail.new(:property => 'attachment', :prop_key => a.id, :value => a.filename)} |
|
184 | 184 | if @issue.save |
|
185 | 185 | # Log spend time |
|
186 | 186 | if current_role.allowed_to?(:log_time) |
|
187 | 187 | @time_entry = TimeEntry.new(:project => @project, :issue => @issue, :user => User.current, :spent_on => Date.today) |
|
188 | 188 | @time_entry.attributes = params[:time_entry] |
|
189 | 189 | @time_entry.save |
|
190 | 190 | end |
|
191 | 191 | if !journal.new_record? |
|
192 | 192 | # Only send notification if something was actually changed |
|
193 | 193 | flash[:notice] = l(:notice_successful_update) |
|
194 | 194 | Mailer.deliver_issue_edit(journal) if Setting.notified_events.include?('issue_updated') |
|
195 | 195 | end |
|
196 | 196 | redirect_to(params[:back_to] || {:action => 'show', :id => @issue}) |
|
197 | 197 | end |
|
198 | 198 | end |
|
199 | 199 | rescue ActiveRecord::StaleObjectError |
|
200 | 200 | # Optimistic locking exception |
|
201 | 201 | flash.now[:error] = l(:notice_locking_conflict) |
|
202 | 202 | end |
|
203 | 203 | |
|
204 | 204 | # Bulk edit a set of issues |
|
205 | 205 | def bulk_edit |
|
206 | 206 | if request.post? |
|
207 | 207 | status = params[:status_id].blank? ? nil : IssueStatus.find_by_id(params[:status_id]) |
|
208 | 208 | priority = params[:priority_id].blank? ? nil : Enumeration.find_by_id(params[:priority_id]) |
|
209 | 209 | assigned_to = params[:assigned_to_id].blank? ? nil : User.find_by_id(params[:assigned_to_id]) |
|
210 | 210 | category = params[:category_id].blank? ? nil : @project.issue_categories.find_by_id(params[:category_id]) |
|
211 | 211 | fixed_version = params[:fixed_version_id].blank? ? nil : @project.versions.find_by_id(params[:fixed_version_id]) |
|
212 | 212 | |
|
213 | 213 | unsaved_issue_ids = [] |
|
214 | 214 | @issues.each do |issue| |
|
215 | 215 | journal = issue.init_journal(User.current, params[:notes]) |
|
216 | 216 | issue.priority = priority if priority |
|
217 | 217 | issue.assigned_to = assigned_to if assigned_to || params[:assigned_to_id] == 'none' |
|
218 | 218 | issue.category = category if category |
|
219 | 219 | issue.fixed_version = fixed_version if fixed_version |
|
220 | 220 | issue.start_date = params[:start_date] unless params[:start_date].blank? |
|
221 | 221 | issue.due_date = params[:due_date] unless params[:due_date].blank? |
|
222 | 222 | issue.done_ratio = params[:done_ratio] unless params[:done_ratio].blank? |
|
223 | 223 | # Don't save any change to the issue if the user is not authorized to apply the requested status |
|
224 | 224 | if (status.nil? || (issue.status.new_status_allowed_to?(status, current_role, issue.tracker) && issue.status = status)) && issue.save |
|
225 | 225 | # Send notification for each issue (if changed) |
|
226 | 226 | Mailer.deliver_issue_edit(journal) if journal.details.any? && Setting.notified_events.include?('issue_updated') |
|
227 | 227 | else |
|
228 | 228 | # Keep unsaved issue ids to display them in flash error |
|
229 | 229 | unsaved_issue_ids << issue.id |
|
230 | 230 | end |
|
231 | 231 | end |
|
232 | 232 | if unsaved_issue_ids.empty? |
|
233 | 233 | flash[:notice] = l(:notice_successful_update) unless @issues.empty? |
|
234 | 234 | else |
|
235 | 235 | flash[:error] = l(:notice_failed_to_save_issues, unsaved_issue_ids.size, @issues.size, '#' + unsaved_issue_ids.join(', #')) |
|
236 | 236 | end |
|
237 | 237 | redirect_to :controller => 'issues', :action => 'index', :project_id => @project |
|
238 | 238 | return |
|
239 | 239 | end |
|
240 | 240 | # Find potential statuses the user could be allowed to switch issues to |
|
241 | 241 | @available_statuses = Workflow.find(:all, :include => :new_status, |
|
242 | 242 | :conditions => {:role_id => current_role.id}).collect(&:new_status).compact.uniq |
|
243 | 243 | end |
|
244 | 244 | |
|
245 | 245 | def move |
|
246 | 246 | @allowed_projects = [] |
|
247 | 247 | # find projects to which the user is allowed to move the issue |
|
248 | 248 | if User.current.admin? |
|
249 | 249 | # admin is allowed to move issues to any active (visible) project |
|
250 | 250 | @allowed_projects = Project.find(:all, :conditions => Project.visible_by(User.current), :order => 'name') |
|
251 | 251 | else |
|
252 | 252 | User.current.memberships.each {|m| @allowed_projects << m.project if m.role.allowed_to?(:move_issues)} |
|
253 | 253 | end |
|
254 | 254 | @target_project = @allowed_projects.detect {|p| p.id.to_s == params[:new_project_id]} if params[:new_project_id] |
|
255 | 255 | @target_project ||= @project |
|
256 | 256 | @trackers = @target_project.trackers |
|
257 | 257 | if request.post? |
|
258 | 258 | new_tracker = params[:new_tracker_id].blank? ? nil : @target_project.trackers.find_by_id(params[:new_tracker_id]) |
|
259 | 259 | unsaved_issue_ids = [] |
|
260 | 260 | @issues.each do |issue| |
|
261 | 261 | unsaved_issue_ids << issue.id unless issue.move_to(@target_project, new_tracker) |
|
262 | 262 | end |
|
263 | 263 | if unsaved_issue_ids.empty? |
|
264 | 264 | flash[:notice] = l(:notice_successful_update) unless @issues.empty? |
|
265 | 265 | else |
|
266 | 266 | flash[:error] = l(:notice_failed_to_save_issues, unsaved_issue_ids.size, @issues.size, '#' + unsaved_issue_ids.join(', #')) |
|
267 | 267 | end |
|
268 | 268 | redirect_to :controller => 'issues', :action => 'index', :project_id => @project |
|
269 | 269 | return |
|
270 | 270 | end |
|
271 | 271 | render :layout => false if request.xhr? |
|
272 | 272 | end |
|
273 | 273 | |
|
274 | 274 | def destroy |
|
275 | 275 | @hours = TimeEntry.sum(:hours, :conditions => ['issue_id IN (?)', @issues]).to_f |
|
276 | 276 | if @hours > 0 |
|
277 | 277 | case params[:todo] |
|
278 | 278 | when 'destroy' |
|
279 | 279 | # nothing to do |
|
280 | 280 | when 'nullify' |
|
281 | 281 | TimeEntry.update_all('issue_id = NULL', ['issue_id IN (?)', @issues]) |
|
282 | 282 | when 'reassign' |
|
283 | 283 | reassign_to = @project.issues.find_by_id(params[:reassign_to_id]) |
|
284 | 284 | if reassign_to.nil? |
|
285 | 285 | flash.now[:error] = l(:error_issue_not_found_in_project) |
|
286 | 286 | return |
|
287 | 287 | else |
|
288 | 288 | TimeEntry.update_all("issue_id = #{reassign_to.id}", ['issue_id IN (?)', @issues]) |
|
289 | 289 | end |
|
290 | 290 | else |
|
291 | 291 | # display the destroy form |
|
292 | 292 | return |
|
293 | 293 | end |
|
294 | 294 | end |
|
295 | 295 | @issues.each(&:destroy) |
|
296 | 296 | redirect_to :action => 'index', :project_id => @project |
|
297 | 297 | end |
|
298 | 298 | |
|
299 | 299 | def destroy_attachment |
|
300 | 300 | a = @issue.attachments.find(params[:attachment_id]) |
|
301 | 301 | a.destroy |
|
302 | 302 | journal = @issue.init_journal(User.current) |
|
303 | 303 | journal.details << JournalDetail.new(:property => 'attachment', |
|
304 | 304 | :prop_key => a.id, |
|
305 | 305 | :old_value => a.filename) |
|
306 | 306 | journal.save |
|
307 | 307 | redirect_to :action => 'show', :id => @issue |
|
308 | 308 | end |
|
309 | 309 | |
|
310 | 310 | def context_menu |
|
311 | 311 | @issues = Issue.find_all_by_id(params[:ids], :include => :project) |
|
312 | 312 | if (@issues.size == 1) |
|
313 | 313 | @issue = @issues.first |
|
314 | 314 | @allowed_statuses = @issue.new_statuses_allowed_to(User.current) |
|
315 | 315 | @assignables = @issue.assignable_users |
|
316 | 316 | @assignables << @issue.assigned_to if @issue.assigned_to && !@assignables.include?(@issue.assigned_to) |
|
317 | 317 | end |
|
318 | 318 | projects = @issues.collect(&:project).compact.uniq |
|
319 | 319 | @project = projects.first if projects.size == 1 |
|
320 | 320 | |
|
321 | 321 | @can = {:edit => (@project && User.current.allowed_to?(:edit_issues, @project)), |
|
322 | 322 | :update => (@issue && (User.current.allowed_to?(:edit_issues, @project) || (User.current.allowed_to?(:change_status, @project) && !@allowed_statuses.empty?))), |
|
323 | 323 | :move => (@project && User.current.allowed_to?(:move_issues, @project)), |
|
324 | 324 | :copy => (@issue && @project.trackers.include?(@issue.tracker) && User.current.allowed_to?(:add_issues, @project)), |
|
325 | 325 | :delete => (@project && User.current.allowed_to?(:delete_issues, @project)) |
|
326 | 326 | } |
|
327 | 327 | |
|
328 | 328 | @priorities = Enumeration.get_values('IPRI').reverse |
|
329 | 329 | @statuses = IssueStatus.find(:all, :order => 'position') |
|
330 | 330 | @back = request.env['HTTP_REFERER'] |
|
331 | 331 | |
|
332 | 332 | render :layout => false |
|
333 | 333 | end |
|
334 | 334 | |
|
335 | 335 | def update_form |
|
336 | 336 | @issue = Issue.new(params[:issue]) |
|
337 | 337 | render :action => :new, :layout => false |
|
338 | 338 | end |
|
339 | 339 | |
|
340 | 340 | def preview |
|
341 | 341 | issue = @project.issues.find_by_id(params[:id]) |
|
342 | 342 | @attachements = issue.attachments if issue |
|
343 | 343 | @text = params[:notes] || (params[:issue] ? params[:issue][:description] : nil) |
|
344 | 344 | render :partial => 'common/preview' |
|
345 | 345 | end |
|
346 | 346 | |
|
347 | 347 | private |
|
348 | 348 | def find_issue |
|
349 | 349 | @issue = Issue.find(params[:id], :include => [:project, :tracker, :status, :author, :priority, :category]) |
|
350 | 350 | @project = @issue.project |
|
351 | 351 | rescue ActiveRecord::RecordNotFound |
|
352 | 352 | render_404 |
|
353 | 353 | end |
|
354 | 354 | |
|
355 | 355 | # Filter for bulk operations |
|
356 | 356 | def find_issues |
|
357 | 357 | @issues = Issue.find_all_by_id(params[:id] || params[:ids]) |
|
358 | 358 | raise ActiveRecord::RecordNotFound if @issues.empty? |
|
359 | 359 | projects = @issues.collect(&:project).compact.uniq |
|
360 | 360 | if projects.size == 1 |
|
361 | 361 | @project = projects.first |
|
362 | 362 | else |
|
363 | 363 | # TODO: let users bulk edit/move/destroy issues from different projects |
|
364 | 364 | render_error 'Can not bulk edit/move/destroy issues from different projects' and return false |
|
365 | 365 | end |
|
366 | 366 | rescue ActiveRecord::RecordNotFound |
|
367 | 367 | render_404 |
|
368 | 368 | end |
|
369 | 369 | |
|
370 | 370 | def find_project |
|
371 | 371 | @project = Project.find(params[:project_id]) |
|
372 | 372 | rescue ActiveRecord::RecordNotFound |
|
373 | 373 | render_404 |
|
374 | 374 | end |
|
375 | 375 | |
|
376 | 376 | def find_optional_project |
|
377 | 377 | return true unless params[:project_id] |
|
378 | 378 | @project = Project.find(params[:project_id]) |
|
379 | 379 | authorize |
|
380 | 380 | rescue ActiveRecord::RecordNotFound |
|
381 | 381 | render_404 |
|
382 | 382 | end |
|
383 | 383 | |
|
384 | 384 | # Retrieve query from session or build a new query |
|
385 | 385 | def retrieve_query |
|
386 | 386 | if !params[:query_id].blank? |
|
387 | 387 | @query = Query.find(params[:query_id], :conditions => {:project_id => (@project ? @project.id : nil)}) |
|
388 | 388 | session[:query] = {:id => @query.id, :project_id => @query.project_id} |
|
389 | 389 | else |
|
390 | 390 | if params[:set_filter] || session[:query].nil? || session[:query][:project_id] != (@project ? @project.id : nil) |
|
391 | 391 | # Give it a name, required to be valid |
|
392 | 392 | @query = Query.new(:name => "_") |
|
393 | 393 | @query.project = @project |
|
394 | 394 | if params[:fields] and params[:fields].is_a? Array |
|
395 | 395 | params[:fields].each do |field| |
|
396 | 396 | @query.add_filter(field, params[:operators][field], params[:values][field]) |
|
397 | 397 | end |
|
398 | 398 | else |
|
399 | 399 | @query.available_filters.keys.each do |field| |
|
400 | 400 | @query.add_short_filter(field, params[field]) if params[field] |
|
401 | 401 | end |
|
402 | 402 | end |
|
403 | 403 | session[:query] = {:project_id => @query.project_id, :filters => @query.filters} |
|
404 | 404 | else |
|
405 | 405 | @query = Query.find_by_id(session[:query][:id]) if session[:query][:id] |
|
406 | 406 | @query ||= Query.new(:name => "_", :project => @project, :filters => session[:query][:filters]) |
|
407 | 407 | end |
|
408 | 408 | end |
|
409 | 409 | end |
|
410 | 410 | end |
General Comments 0
You need to be logged in to leave comments.
Login now