The requested changes are too big and content was truncated. Show full diff
@@ -1,442 +1,443 | |||
|
1 | 1 | # Redmine - project management software |
|
2 | 2 | # Copyright (C) 2006-2012 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, :create] |
|
20 | 20 | default_search_scope :issues |
|
21 | 21 | |
|
22 | 22 | before_filter :find_issue, :only => [:show, :edit, :update] |
|
23 | 23 | before_filter :find_issues, :only => [:bulk_edit, :bulk_update, :destroy] |
|
24 | 24 | before_filter :find_project, :only => [:new, :create] |
|
25 | 25 | before_filter :authorize, :except => [:index] |
|
26 | 26 | before_filter :find_optional_project, :only => [:index] |
|
27 | 27 | before_filter :check_for_default_issue_status, :only => [:new, :create] |
|
28 | 28 | before_filter :build_new_issue_from_params, :only => [:new, :create] |
|
29 | 29 | accept_rss_auth :index, :show |
|
30 | 30 | accept_api_auth :index, :show, :create, :update, :destroy |
|
31 | 31 | |
|
32 | 32 | rescue_from Query::StatementInvalid, :with => :query_statement_invalid |
|
33 | 33 | |
|
34 | 34 | helper :journals |
|
35 | 35 | helper :projects |
|
36 | 36 | include ProjectsHelper |
|
37 | 37 | helper :custom_fields |
|
38 | 38 | include CustomFieldsHelper |
|
39 | 39 | helper :issue_relations |
|
40 | 40 | include IssueRelationsHelper |
|
41 | 41 | helper :watchers |
|
42 | 42 | include WatchersHelper |
|
43 | 43 | helper :attachments |
|
44 | 44 | include AttachmentsHelper |
|
45 | 45 | helper :queries |
|
46 | 46 | include QueriesHelper |
|
47 | 47 | helper :repositories |
|
48 | 48 | include RepositoriesHelper |
|
49 | 49 | helper :sort |
|
50 | 50 | include SortHelper |
|
51 | 51 | include IssuesHelper |
|
52 | 52 | helper :timelog |
|
53 | 53 | include Redmine::Export::PDF |
|
54 | 54 | |
|
55 | 55 | def index |
|
56 | 56 | retrieve_query |
|
57 | 57 | sort_init(@query.sort_criteria.empty? ? [['id', 'desc']] : @query.sort_criteria) |
|
58 | 58 | sort_update(@query.sortable_columns) |
|
59 | @query.sort_criteria = sort_criteria.to_a | |
|
59 | 60 | |
|
60 | 61 | if @query.valid? |
|
61 | 62 | case params[:format] |
|
62 | 63 | when 'csv', 'pdf' |
|
63 | 64 | @limit = Setting.issues_export_limit.to_i |
|
64 | 65 | when 'atom' |
|
65 | 66 | @limit = Setting.feeds_limit.to_i |
|
66 | 67 | when 'xml', 'json' |
|
67 | 68 | @offset, @limit = api_offset_and_limit |
|
68 | 69 | else |
|
69 | 70 | @limit = per_page_option |
|
70 | 71 | end |
|
71 | 72 | |
|
72 | 73 | @issue_count = @query.issue_count |
|
73 | 74 | @issue_pages = Paginator.new self, @issue_count, @limit, params['page'] |
|
74 | 75 | @offset ||= @issue_pages.current.offset |
|
75 | 76 | @issues = @query.issues(:include => [:assigned_to, :tracker, :priority, :category, :fixed_version], |
|
76 | 77 | :order => sort_clause, |
|
77 | 78 | :offset => @offset, |
|
78 | 79 | :limit => @limit) |
|
79 | 80 | @issue_count_by_group = @query.issue_count_by_group |
|
80 | 81 | |
|
81 | 82 | respond_to do |format| |
|
82 | 83 | format.html { render :template => 'issues/index', :layout => !request.xhr? } |
|
83 | 84 | format.api { |
|
84 | 85 | Issue.load_visible_relations(@issues) if include_in_api_response?('relations') |
|
85 | 86 | } |
|
86 | 87 | format.atom { render_feed(@issues, :title => "#{@project || Setting.app_title}: #{l(:label_issue_plural)}") } |
|
87 | 88 | format.csv { send_data(issues_to_csv(@issues, @project, @query, params), :type => 'text/csv; header=present', :filename => 'export.csv') } |
|
88 | 89 | format.pdf { send_data(issues_to_pdf(@issues, @project, @query), :type => 'application/pdf', :filename => 'export.pdf') } |
|
89 | 90 | end |
|
90 | 91 | else |
|
91 | 92 | respond_to do |format| |
|
92 | 93 | format.html { render(:template => 'issues/index', :layout => !request.xhr?) } |
|
93 | 94 | format.any(:atom, :csv, :pdf) { render(:nothing => true) } |
|
94 | 95 | format.api { render_validation_errors(@query) } |
|
95 | 96 | end |
|
96 | 97 | end |
|
97 | 98 | rescue ActiveRecord::RecordNotFound |
|
98 | 99 | render_404 |
|
99 | 100 | end |
|
100 | 101 | |
|
101 | 102 | def show |
|
102 | 103 | @journals = @issue.journals.includes(:user, :details).reorder("#{Journal.table_name}.id ASC").all |
|
103 | 104 | @journals.each_with_index {|j,i| j.indice = i+1} |
|
104 | 105 | @journals.reject!(&:private_notes?) unless User.current.allowed_to?(:view_private_notes, @issue.project) |
|
105 | 106 | @journals.reverse! if User.current.wants_comments_in_reverse_order? |
|
106 | 107 | |
|
107 | 108 | @changesets = @issue.changesets.visible.all |
|
108 | 109 | @changesets.reverse! if User.current.wants_comments_in_reverse_order? |
|
109 | 110 | |
|
110 | 111 | @relations = @issue.relations.select {|r| r.other_issue(@issue) && r.other_issue(@issue).visible? } |
|
111 | 112 | @allowed_statuses = @issue.new_statuses_allowed_to(User.current) |
|
112 | 113 | @edit_allowed = User.current.allowed_to?(:edit_issues, @project) |
|
113 | 114 | @priorities = IssuePriority.active |
|
114 | 115 | @time_entry = TimeEntry.new(:issue => @issue, :project => @issue.project) |
|
115 | 116 | respond_to do |format| |
|
116 | 117 | format.html { |
|
117 | 118 | retrieve_previous_and_next_issue_ids |
|
118 | 119 | render :template => 'issues/show' |
|
119 | 120 | } |
|
120 | 121 | format.api |
|
121 | 122 | format.atom { render :template => 'journals/index', :layout => false, :content_type => 'application/atom+xml' } |
|
122 | 123 | format.pdf { |
|
123 | 124 | pdf = issue_to_pdf(@issue, :journals => @journals) |
|
124 | 125 | send_data(pdf, :type => 'application/pdf', :filename => "#{@project.identifier}-#{@issue.id}.pdf") |
|
125 | 126 | } |
|
126 | 127 | end |
|
127 | 128 | end |
|
128 | 129 | |
|
129 | 130 | # Add a new issue |
|
130 | 131 | # The new issue will be created from an existing one if copy_from parameter is given |
|
131 | 132 | def new |
|
132 | 133 | respond_to do |format| |
|
133 | 134 | format.html { render :action => 'new', :layout => !request.xhr? } |
|
134 | 135 | format.js { render :partial => 'update_form' } |
|
135 | 136 | end |
|
136 | 137 | end |
|
137 | 138 | |
|
138 | 139 | def create |
|
139 | 140 | call_hook(:controller_issues_new_before_save, { :params => params, :issue => @issue }) |
|
140 | 141 | @issue.save_attachments(params[:attachments] || (params[:issue] && params[:issue][:uploads])) |
|
141 | 142 | if @issue.save |
|
142 | 143 | call_hook(:controller_issues_new_after_save, { :params => params, :issue => @issue}) |
|
143 | 144 | respond_to do |format| |
|
144 | 145 | format.html { |
|
145 | 146 | render_attachment_warning_if_needed(@issue) |
|
146 | 147 | flash[:notice] = l(:notice_issue_successful_create, :id => view_context.link_to("##{@issue.id}", issue_path(@issue), :title => @issue.subject)) |
|
147 | 148 | redirect_to(params[:continue] ? { :action => 'new', :project_id => @issue.project, :issue => {:tracker_id => @issue.tracker, :parent_issue_id => @issue.parent_issue_id}.reject {|k,v| v.nil?} } : |
|
148 | 149 | { :action => 'show', :id => @issue }) |
|
149 | 150 | } |
|
150 | 151 | format.api { render :action => 'show', :status => :created, :location => issue_url(@issue) } |
|
151 | 152 | end |
|
152 | 153 | return |
|
153 | 154 | else |
|
154 | 155 | respond_to do |format| |
|
155 | 156 | format.html { render :action => 'new' } |
|
156 | 157 | format.api { render_validation_errors(@issue) } |
|
157 | 158 | end |
|
158 | 159 | end |
|
159 | 160 | end |
|
160 | 161 | |
|
161 | 162 | def edit |
|
162 | 163 | return unless update_issue_from_params |
|
163 | 164 | |
|
164 | 165 | respond_to do |format| |
|
165 | 166 | format.html { } |
|
166 | 167 | format.xml { } |
|
167 | 168 | end |
|
168 | 169 | end |
|
169 | 170 | |
|
170 | 171 | def update |
|
171 | 172 | return unless update_issue_from_params |
|
172 | 173 | @issue.save_attachments(params[:attachments] || (params[:issue] && params[:issue][:uploads])) |
|
173 | 174 | saved = false |
|
174 | 175 | begin |
|
175 | 176 | saved = @issue.save_issue_with_child_records(params, @time_entry) |
|
176 | 177 | rescue ActiveRecord::StaleObjectError |
|
177 | 178 | @conflict = true |
|
178 | 179 | if params[:last_journal_id] |
|
179 | 180 | @conflict_journals = @issue.journals_after(params[:last_journal_id]).all |
|
180 | 181 | @conflict_journals.reject!(&:private_notes?) unless User.current.allowed_to?(:view_private_notes, @issue.project) |
|
181 | 182 | end |
|
182 | 183 | end |
|
183 | 184 | |
|
184 | 185 | if saved |
|
185 | 186 | render_attachment_warning_if_needed(@issue) |
|
186 | 187 | flash[:notice] = l(:notice_successful_update) unless @issue.current_journal.new_record? |
|
187 | 188 | |
|
188 | 189 | respond_to do |format| |
|
189 | 190 | format.html { redirect_back_or_default({:action => 'show', :id => @issue}) } |
|
190 | 191 | format.api { render_api_ok } |
|
191 | 192 | end |
|
192 | 193 | else |
|
193 | 194 | respond_to do |format| |
|
194 | 195 | format.html { render :action => 'edit' } |
|
195 | 196 | format.api { render_validation_errors(@issue) } |
|
196 | 197 | end |
|
197 | 198 | end |
|
198 | 199 | end |
|
199 | 200 | |
|
200 | 201 | # Bulk edit/copy a set of issues |
|
201 | 202 | def bulk_edit |
|
202 | 203 | @issues.sort! |
|
203 | 204 | @copy = params[:copy].present? |
|
204 | 205 | @notes = params[:notes] |
|
205 | 206 | |
|
206 | 207 | if User.current.allowed_to?(:move_issues, @projects) |
|
207 | 208 | @allowed_projects = Issue.allowed_target_projects_on_move |
|
208 | 209 | if params[:issue] |
|
209 | 210 | @target_project = @allowed_projects.detect {|p| p.id.to_s == params[:issue][:project_id].to_s} |
|
210 | 211 | if @target_project |
|
211 | 212 | target_projects = [@target_project] |
|
212 | 213 | end |
|
213 | 214 | end |
|
214 | 215 | end |
|
215 | 216 | target_projects ||= @projects |
|
216 | 217 | |
|
217 | 218 | if @copy |
|
218 | 219 | @available_statuses = [IssueStatus.default] |
|
219 | 220 | else |
|
220 | 221 | @available_statuses = @issues.map(&:new_statuses_allowed_to).reduce(:&) |
|
221 | 222 | end |
|
222 | 223 | @custom_fields = target_projects.map{|p|p.all_issue_custom_fields}.reduce(:&) |
|
223 | 224 | @assignables = target_projects.map(&:assignable_users).reduce(:&) |
|
224 | 225 | @trackers = target_projects.map(&:trackers).reduce(:&) |
|
225 | 226 | @versions = target_projects.map {|p| p.shared_versions.open}.reduce(:&) |
|
226 | 227 | @categories = target_projects.map {|p| p.issue_categories}.reduce(:&) |
|
227 | 228 | if @copy |
|
228 | 229 | @attachments_present = @issues.detect {|i| i.attachments.any?}.present? |
|
229 | 230 | @subtasks_present = @issues.detect {|i| !i.leaf?}.present? |
|
230 | 231 | end |
|
231 | 232 | |
|
232 | 233 | @safe_attributes = @issues.map(&:safe_attribute_names).reduce(:&) |
|
233 | 234 | render :layout => false if request.xhr? |
|
234 | 235 | end |
|
235 | 236 | |
|
236 | 237 | def bulk_update |
|
237 | 238 | @issues.sort! |
|
238 | 239 | @copy = params[:copy].present? |
|
239 | 240 | attributes = parse_params_for_bulk_issue_attributes(params) |
|
240 | 241 | |
|
241 | 242 | unsaved_issue_ids = [] |
|
242 | 243 | moved_issues = [] |
|
243 | 244 | |
|
244 | 245 | if @copy && params[:copy_subtasks].present? |
|
245 | 246 | # Descendant issues will be copied with the parent task |
|
246 | 247 | # Don't copy them twice |
|
247 | 248 | @issues.reject! {|issue| @issues.detect {|other| issue.is_descendant_of?(other)}} |
|
248 | 249 | end |
|
249 | 250 | |
|
250 | 251 | @issues.each do |issue| |
|
251 | 252 | issue.reload |
|
252 | 253 | if @copy |
|
253 | 254 | issue = issue.copy({}, |
|
254 | 255 | :attachments => params[:copy_attachments].present?, |
|
255 | 256 | :subtasks => params[:copy_subtasks].present? |
|
256 | 257 | ) |
|
257 | 258 | end |
|
258 | 259 | journal = issue.init_journal(User.current, params[:notes]) |
|
259 | 260 | issue.safe_attributes = attributes |
|
260 | 261 | call_hook(:controller_issues_bulk_edit_before_save, { :params => params, :issue => issue }) |
|
261 | 262 | if issue.save |
|
262 | 263 | moved_issues << issue |
|
263 | 264 | else |
|
264 | 265 | # Keep unsaved issue ids to display them in flash error |
|
265 | 266 | unsaved_issue_ids << issue.id |
|
266 | 267 | end |
|
267 | 268 | end |
|
268 | 269 | set_flash_from_bulk_issue_save(@issues, unsaved_issue_ids) |
|
269 | 270 | |
|
270 | 271 | if params[:follow] |
|
271 | 272 | if @issues.size == 1 && moved_issues.size == 1 |
|
272 | 273 | redirect_to :controller => 'issues', :action => 'show', :id => moved_issues.first |
|
273 | 274 | elsif moved_issues.map(&:project).uniq.size == 1 |
|
274 | 275 | redirect_to :controller => 'issues', :action => 'index', :project_id => moved_issues.map(&:project).first |
|
275 | 276 | end |
|
276 | 277 | else |
|
277 | 278 | redirect_back_or_default({:controller => 'issues', :action => 'index', :project_id => @project}) |
|
278 | 279 | end |
|
279 | 280 | end |
|
280 | 281 | |
|
281 | 282 | def destroy |
|
282 | 283 | @hours = TimeEntry.sum(:hours, :conditions => ['issue_id IN (?)', @issues]).to_f |
|
283 | 284 | if @hours > 0 |
|
284 | 285 | case params[:todo] |
|
285 | 286 | when 'destroy' |
|
286 | 287 | # nothing to do |
|
287 | 288 | when 'nullify' |
|
288 | 289 | TimeEntry.update_all('issue_id = NULL', ['issue_id IN (?)', @issues]) |
|
289 | 290 | when 'reassign' |
|
290 | 291 | reassign_to = @project.issues.find_by_id(params[:reassign_to_id]) |
|
291 | 292 | if reassign_to.nil? |
|
292 | 293 | flash.now[:error] = l(:error_issue_not_found_in_project) |
|
293 | 294 | return |
|
294 | 295 | else |
|
295 | 296 | TimeEntry.update_all("issue_id = #{reassign_to.id}", ['issue_id IN (?)', @issues]) |
|
296 | 297 | end |
|
297 | 298 | else |
|
298 | 299 | # display the destroy form if it's a user request |
|
299 | 300 | return unless api_request? |
|
300 | 301 | end |
|
301 | 302 | end |
|
302 | 303 | @issues.each do |issue| |
|
303 | 304 | begin |
|
304 | 305 | issue.reload.destroy |
|
305 | 306 | rescue ::ActiveRecord::RecordNotFound # raised by #reload if issue no longer exists |
|
306 | 307 | # nothing to do, issue was already deleted (eg. by a parent) |
|
307 | 308 | end |
|
308 | 309 | end |
|
309 | 310 | respond_to do |format| |
|
310 | 311 | format.html { redirect_back_or_default(:action => 'index', :project_id => @project) } |
|
311 | 312 | format.api { render_api_ok } |
|
312 | 313 | end |
|
313 | 314 | end |
|
314 | 315 | |
|
315 | 316 | private |
|
316 | 317 | def find_issue |
|
317 | 318 | # Issue.visible.find(...) can not be used to redirect user to the login form |
|
318 | 319 | # if the issue actually exists but requires authentication |
|
319 | 320 | @issue = Issue.find(params[:id], :include => [:project, :tracker, :status, :author, :priority, :category]) |
|
320 | 321 | unless @issue.visible? |
|
321 | 322 | deny_access |
|
322 | 323 | return |
|
323 | 324 | end |
|
324 | 325 | @project = @issue.project |
|
325 | 326 | rescue ActiveRecord::RecordNotFound |
|
326 | 327 | render_404 |
|
327 | 328 | end |
|
328 | 329 | |
|
329 | 330 | def find_project |
|
330 | 331 | project_id = params[:project_id] || (params[:issue] && params[:issue][:project_id]) |
|
331 | 332 | @project = Project.find(project_id) |
|
332 | 333 | rescue ActiveRecord::RecordNotFound |
|
333 | 334 | render_404 |
|
334 | 335 | end |
|
335 | 336 | |
|
336 | 337 | def retrieve_previous_and_next_issue_ids |
|
337 | 338 | retrieve_query_from_session |
|
338 | 339 | if @query |
|
339 | 340 | sort_init(@query.sort_criteria.empty? ? [['id', 'desc']] : @query.sort_criteria) |
|
340 | 341 | sort_update(@query.sortable_columns, 'issues_index_sort') |
|
341 | 342 | limit = 500 |
|
342 | 343 | issue_ids = @query.issue_ids(:order => sort_clause, :limit => (limit + 1), :include => [:assigned_to, :tracker, :priority, :category, :fixed_version]) |
|
343 | 344 | if (idx = issue_ids.index(@issue.id)) && idx < limit |
|
344 | 345 | if issue_ids.size < 500 |
|
345 | 346 | @issue_position = idx + 1 |
|
346 | 347 | @issue_count = issue_ids.size |
|
347 | 348 | end |
|
348 | 349 | @prev_issue_id = issue_ids[idx - 1] if idx > 0 |
|
349 | 350 | @next_issue_id = issue_ids[idx + 1] if idx < (issue_ids.size - 1) |
|
350 | 351 | end |
|
351 | 352 | end |
|
352 | 353 | end |
|
353 | 354 | |
|
354 | 355 | # Used by #edit and #update to set some common instance variables |
|
355 | 356 | # from the params |
|
356 | 357 | # TODO: Refactor, not everything in here is needed by #edit |
|
357 | 358 | def update_issue_from_params |
|
358 | 359 | @edit_allowed = User.current.allowed_to?(:edit_issues, @project) |
|
359 | 360 | @time_entry = TimeEntry.new(:issue => @issue, :project => @issue.project) |
|
360 | 361 | @time_entry.attributes = params[:time_entry] |
|
361 | 362 | |
|
362 | 363 | @issue.init_journal(User.current) |
|
363 | 364 | |
|
364 | 365 | issue_attributes = params[:issue] |
|
365 | 366 | if issue_attributes && params[:conflict_resolution] |
|
366 | 367 | case params[:conflict_resolution] |
|
367 | 368 | when 'overwrite' |
|
368 | 369 | issue_attributes = issue_attributes.dup |
|
369 | 370 | issue_attributes.delete(:lock_version) |
|
370 | 371 | when 'add_notes' |
|
371 | 372 | issue_attributes = issue_attributes.slice(:notes) |
|
372 | 373 | when 'cancel' |
|
373 | 374 | redirect_to issue_path(@issue) |
|
374 | 375 | return false |
|
375 | 376 | end |
|
376 | 377 | end |
|
377 | 378 | @issue.safe_attributes = issue_attributes |
|
378 | 379 | @priorities = IssuePriority.active |
|
379 | 380 | @allowed_statuses = @issue.new_statuses_allowed_to(User.current) |
|
380 | 381 | true |
|
381 | 382 | end |
|
382 | 383 | |
|
383 | 384 | # TODO: Refactor, lots of extra code in here |
|
384 | 385 | # TODO: Changing tracker on an existing issue should not trigger this |
|
385 | 386 | def build_new_issue_from_params |
|
386 | 387 | if params[:id].blank? |
|
387 | 388 | @issue = Issue.new |
|
388 | 389 | if params[:copy_from] |
|
389 | 390 | begin |
|
390 | 391 | @copy_from = Issue.visible.find(params[:copy_from]) |
|
391 | 392 | @copy_attachments = params[:copy_attachments].present? || request.get? |
|
392 | 393 | @copy_subtasks = params[:copy_subtasks].present? || request.get? |
|
393 | 394 | @issue.copy_from(@copy_from, :attachments => @copy_attachments, :subtasks => @copy_subtasks) |
|
394 | 395 | rescue ActiveRecord::RecordNotFound |
|
395 | 396 | render_404 |
|
396 | 397 | return |
|
397 | 398 | end |
|
398 | 399 | end |
|
399 | 400 | @issue.project = @project |
|
400 | 401 | else |
|
401 | 402 | @issue = @project.issues.visible.find(params[:id]) |
|
402 | 403 | end |
|
403 | 404 | |
|
404 | 405 | @issue.project = @project |
|
405 | 406 | @issue.author ||= User.current |
|
406 | 407 | # Tracker must be set before custom field values |
|
407 | 408 | @issue.tracker ||= @project.trackers.find((params[:issue] && params[:issue][:tracker_id]) || params[:tracker_id] || :first) |
|
408 | 409 | if @issue.tracker.nil? |
|
409 | 410 | render_error l(:error_no_tracker_in_project) |
|
410 | 411 | return false |
|
411 | 412 | end |
|
412 | 413 | @issue.start_date ||= Date.today if Setting.default_issue_start_date_to_creation_date? |
|
413 | 414 | @issue.safe_attributes = params[:issue] |
|
414 | 415 | |
|
415 | 416 | @priorities = IssuePriority.active |
|
416 | 417 | @allowed_statuses = @issue.new_statuses_allowed_to(User.current, true) |
|
417 | 418 | @available_watchers = (@issue.project.users.sort + @issue.watcher_users).uniq |
|
418 | 419 | end |
|
419 | 420 | |
|
420 | 421 | def check_for_default_issue_status |
|
421 | 422 | if IssueStatus.default.nil? |
|
422 | 423 | render_error l(:error_no_default_issue_status) |
|
423 | 424 | return false |
|
424 | 425 | end |
|
425 | 426 | end |
|
426 | 427 | |
|
427 | 428 | def parse_params_for_bulk_issue_attributes(params) |
|
428 | 429 | attributes = (params[:issue] || {}).reject {|k,v| v.blank?} |
|
429 | 430 | attributes.keys.each {|k| attributes[k] = '' if attributes[k] == 'none'} |
|
430 | 431 | if custom = attributes[:custom_field_values] |
|
431 | 432 | custom.reject! {|k,v| v.blank?} |
|
432 | 433 | custom.keys.each do |k| |
|
433 | 434 | if custom[k].is_a?(Array) |
|
434 | 435 | custom[k] << '' if custom[k].delete('__none__') |
|
435 | 436 | else |
|
436 | 437 | custom[k] = '' if custom[k] == '__none__' |
|
437 | 438 | end |
|
438 | 439 | end |
|
439 | 440 | end |
|
440 | 441 | attributes |
|
441 | 442 | end |
|
442 | 443 | end |
@@ -1,234 +1,242 | |||
|
1 | 1 | # encoding: utf-8 |
|
2 | 2 | # |
|
3 | 3 | # Helpers to sort tables using clickable column headers. |
|
4 | 4 | # |
|
5 | 5 | # Author: Stuart Rackham <srackham@methods.co.nz>, March 2005. |
|
6 | 6 | # Jean-Philippe Lang, 2009 |
|
7 | 7 | # License: This source code is released under the MIT license. |
|
8 | 8 | # |
|
9 | 9 | # - Consecutive clicks toggle the column's sort order. |
|
10 | 10 | # - Sort state is maintained by a session hash entry. |
|
11 | 11 | # - CSS classes identify sort column and state. |
|
12 | 12 | # - Typically used in conjunction with the Pagination module. |
|
13 | 13 | # |
|
14 | 14 | # Example code snippets: |
|
15 | 15 | # |
|
16 | 16 | # Controller: |
|
17 | 17 | # |
|
18 | 18 | # helper :sort |
|
19 | 19 | # include SortHelper |
|
20 | 20 | # |
|
21 | 21 | # def list |
|
22 | 22 | # sort_init 'last_name' |
|
23 | 23 | # sort_update %w(first_name last_name) |
|
24 | 24 | # @items = Contact.find_all nil, sort_clause |
|
25 | 25 | # end |
|
26 | 26 | # |
|
27 | 27 | # Controller (using Pagination module): |
|
28 | 28 | # |
|
29 | 29 | # helper :sort |
|
30 | 30 | # include SortHelper |
|
31 | 31 | # |
|
32 | 32 | # def list |
|
33 | 33 | # sort_init 'last_name' |
|
34 | 34 | # sort_update %w(first_name last_name) |
|
35 | 35 | # @contact_pages, @items = paginate :contacts, |
|
36 | 36 | # :order_by => sort_clause, |
|
37 | 37 | # :per_page => 10 |
|
38 | 38 | # end |
|
39 | 39 | # |
|
40 | 40 | # View (table header in list.rhtml): |
|
41 | 41 | # |
|
42 | 42 | # <thead> |
|
43 | 43 | # <tr> |
|
44 | 44 | # <%= sort_header_tag('id', :title => 'Sort by contact ID') %> |
|
45 | 45 | # <%= sort_header_tag('last_name', :caption => 'Name') %> |
|
46 | 46 | # <%= sort_header_tag('phone') %> |
|
47 | 47 | # <%= sort_header_tag('address', :width => 200) %> |
|
48 | 48 | # </tr> |
|
49 | 49 | # </thead> |
|
50 | 50 | # |
|
51 | 51 | # - Introduces instance variables: @sort_default, @sort_criteria |
|
52 | 52 | # - Introduces param :sort |
|
53 | 53 | # |
|
54 | 54 | |
|
55 | 55 | module SortHelper |
|
56 | 56 | class SortCriteria |
|
57 | 57 | |
|
58 | 58 | def initialize |
|
59 | 59 | @criteria = [] |
|
60 | 60 | end |
|
61 | 61 | |
|
62 | 62 | def available_criteria=(criteria) |
|
63 | 63 | unless criteria.is_a?(Hash) |
|
64 | 64 | criteria = criteria.inject({}) {|h,k| h[k] = k; h} |
|
65 | 65 | end |
|
66 | 66 | @available_criteria = criteria |
|
67 | 67 | end |
|
68 | 68 | |
|
69 | 69 | def from_param(param) |
|
70 | 70 | @criteria = param.to_s.split(',').collect {|s| s.split(':')[0..1]} |
|
71 | 71 | normalize! |
|
72 | 72 | end |
|
73 | 73 | |
|
74 | 74 | def criteria=(arg) |
|
75 | 75 | @criteria = arg |
|
76 | 76 | normalize! |
|
77 | 77 | end |
|
78 | 78 | |
|
79 | 79 | def to_param |
|
80 | 80 | @criteria.collect {|k,o| k + (o ? '' : ':desc')}.join(',') |
|
81 | 81 | end |
|
82 | 82 | |
|
83 | 83 | def to_sql |
|
84 | 84 | sql = @criteria.collect do |k,o| |
|
85 | 85 | if s = @available_criteria[k] |
|
86 | 86 | (o ? s.to_a : s.to_a.collect {|c| append_desc(c)}).join(', ') |
|
87 | 87 | end |
|
88 | 88 | end.compact.join(', ') |
|
89 | 89 | sql.blank? ? nil : sql |
|
90 | 90 | end |
|
91 | 91 | |
|
92 | def to_a | |
|
93 | @criteria.dup | |
|
94 | end | |
|
95 | ||
|
92 | 96 | def add!(key, asc) |
|
93 | 97 | @criteria.delete_if {|k,o| k == key} |
|
94 | 98 | @criteria = [[key, asc]] + @criteria |
|
95 | 99 | normalize! |
|
96 | 100 | end |
|
97 | 101 | |
|
98 | 102 | def add(*args) |
|
99 | 103 | r = self.class.new.from_param(to_param) |
|
100 | 104 | r.add!(*args) |
|
101 | 105 | r |
|
102 | 106 | end |
|
103 | 107 | |
|
104 | 108 | def first_key |
|
105 | 109 | @criteria.first && @criteria.first.first |
|
106 | 110 | end |
|
107 | 111 | |
|
108 | 112 | def first_asc? |
|
109 | 113 | @criteria.first && @criteria.first.last |
|
110 | 114 | end |
|
111 | 115 | |
|
112 | 116 | def empty? |
|
113 | 117 | @criteria.empty? |
|
114 | 118 | end |
|
115 | 119 | |
|
116 | 120 | private |
|
117 | 121 | |
|
118 | 122 | def normalize! |
|
119 | 123 | @criteria ||= [] |
|
120 | 124 | @criteria = @criteria.collect {|s| s = s.to_a; [s.first, (s.last == false || s.last == 'desc') ? false : true]} |
|
121 | 125 | @criteria = @criteria.select {|k,o| @available_criteria.has_key?(k)} if @available_criteria |
|
122 | 126 | @criteria.slice!(3) |
|
123 | 127 | self |
|
124 | 128 | end |
|
125 | 129 | |
|
126 | 130 | # Appends DESC to the sort criterion unless it has a fixed order |
|
127 | 131 | def append_desc(criterion) |
|
128 | 132 | if criterion =~ / (asc|desc)$/i |
|
129 | 133 | criterion |
|
130 | 134 | else |
|
131 | 135 | "#{criterion} DESC" |
|
132 | 136 | end |
|
133 | 137 | end |
|
134 | 138 | end |
|
135 | 139 | |
|
136 | 140 | def sort_name |
|
137 | 141 | controller_name + '_' + action_name + '_sort' |
|
138 | 142 | end |
|
139 | 143 | |
|
140 | 144 | # Initializes the default sort. |
|
141 | 145 | # Examples: |
|
142 | 146 | # |
|
143 | 147 | # sort_init 'name' |
|
144 | 148 | # sort_init 'id', 'desc' |
|
145 | 149 | # sort_init ['name', ['id', 'desc']] |
|
146 | 150 | # sort_init [['name', 'desc'], ['id', 'desc']] |
|
147 | 151 | # |
|
148 | 152 | def sort_init(*args) |
|
149 | 153 | case args.size |
|
150 | 154 | when 1 |
|
151 | 155 | @sort_default = args.first.is_a?(Array) ? args.first : [[args.first]] |
|
152 | 156 | when 2 |
|
153 | 157 | @sort_default = [[args.first, args.last]] |
|
154 | 158 | else |
|
155 | 159 | raise ArgumentError |
|
156 | 160 | end |
|
157 | 161 | end |
|
158 | 162 | |
|
159 | 163 | # Updates the sort state. Call this in the controller prior to calling |
|
160 | 164 | # sort_clause. |
|
161 | 165 | # - criteria can be either an array or a hash of allowed keys |
|
162 | 166 | # |
|
163 | 167 | def sort_update(criteria, sort_name=nil) |
|
164 | 168 | sort_name ||= self.sort_name |
|
165 | 169 | @sort_criteria = SortCriteria.new |
|
166 | 170 | @sort_criteria.available_criteria = criteria |
|
167 | 171 | @sort_criteria.from_param(params[:sort] || session[sort_name]) |
|
168 | 172 | @sort_criteria.criteria = @sort_default if @sort_criteria.empty? |
|
169 | 173 | session[sort_name] = @sort_criteria.to_param |
|
170 | 174 | end |
|
171 | 175 | |
|
172 | 176 | # Clears the sort criteria session data |
|
173 | 177 | # |
|
174 | 178 | def sort_clear |
|
175 | 179 | session[sort_name] = nil |
|
176 | 180 | end |
|
177 | 181 | |
|
178 | 182 | # Returns an SQL sort clause corresponding to the current sort state. |
|
179 | 183 | # Use this to sort the controller's table items collection. |
|
180 | 184 | # |
|
181 | 185 | def sort_clause() |
|
182 | 186 | @sort_criteria.to_sql |
|
183 | 187 | end |
|
184 | 188 | |
|
189 | def sort_criteria | |
|
190 | @sort_criteria | |
|
191 | end | |
|
192 | ||
|
185 | 193 | # Returns a link which sorts by the named column. |
|
186 | 194 | # |
|
187 | 195 | # - column is the name of an attribute in the sorted record collection. |
|
188 | 196 | # - the optional caption explicitly specifies the displayed link text. |
|
189 | 197 | # - 2 CSS classes reflect the state of the link: sort and asc or desc |
|
190 | 198 | # |
|
191 | 199 | def sort_link(column, caption, default_order) |
|
192 | 200 | css, order = nil, default_order |
|
193 | 201 | |
|
194 | 202 | if column.to_s == @sort_criteria.first_key |
|
195 | 203 | if @sort_criteria.first_asc? |
|
196 | 204 | css = 'sort asc' |
|
197 | 205 | order = 'desc' |
|
198 | 206 | else |
|
199 | 207 | css = 'sort desc' |
|
200 | 208 | order = 'asc' |
|
201 | 209 | end |
|
202 | 210 | end |
|
203 | 211 | caption = column.to_s.humanize unless caption |
|
204 | 212 | |
|
205 | 213 | sort_options = { :sort => @sort_criteria.add(column.to_s, order).to_param } |
|
206 | 214 | url_options = params.merge(sort_options) |
|
207 | 215 | |
|
208 | 216 | # Add project_id to url_options |
|
209 | 217 | url_options = url_options.merge(:project_id => params[:project_id]) if params.has_key?(:project_id) |
|
210 | 218 | |
|
211 | 219 | link_to_content_update(h(caption), url_options, :class => css) |
|
212 | 220 | end |
|
213 | 221 | |
|
214 | 222 | # Returns a table header <th> tag with a sort link for the named column |
|
215 | 223 | # attribute. |
|
216 | 224 | # |
|
217 | 225 | # Options: |
|
218 | 226 | # :caption The displayed link name (defaults to titleized column name). |
|
219 | 227 | # :title The tag's 'title' attribute (defaults to 'Sort by :caption'). |
|
220 | 228 | # |
|
221 | 229 | # Other options hash entries generate additional table header tag attributes. |
|
222 | 230 | # |
|
223 | 231 | # Example: |
|
224 | 232 | # |
|
225 | 233 | # <%= sort_header_tag('id', :title => 'Sort by contact ID', :width => 40) %> |
|
226 | 234 | # |
|
227 | 235 | def sort_header_tag(column, options = {}) |
|
228 | 236 | caption = options.delete(:caption) || column.to_s.humanize |
|
229 | 237 | default_order = options.delete(:default_order) || 'asc' |
|
230 | 238 | options[:title] = l(:label_sort_by, "\"#{caption}\"") unless options[:title] |
|
231 | 239 | content_tag('th', sort_link(column, caption, default_order), options) |
|
232 | 240 | end |
|
233 | 241 | end |
|
234 | 242 |
@@ -1,1063 +1,1068 | |||
|
1 | 1 | # Redmine - project management software |
|
2 | 2 | # Copyright (C) 2006-2012 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 QueryColumn |
|
19 | 19 | attr_accessor :name, :sortable, :groupable, :default_order |
|
20 | 20 | include Redmine::I18n |
|
21 | 21 | |
|
22 | 22 | def initialize(name, options={}) |
|
23 | 23 | self.name = name |
|
24 | 24 | self.sortable = options[:sortable] |
|
25 | 25 | self.groupable = options[:groupable] || false |
|
26 | 26 | if groupable == true |
|
27 | 27 | self.groupable = name.to_s |
|
28 | 28 | end |
|
29 | 29 | self.default_order = options[:default_order] |
|
30 | 30 | @caption_key = options[:caption] || "field_#{name}" |
|
31 | 31 | end |
|
32 | 32 | |
|
33 | 33 | def caption |
|
34 | 34 | l(@caption_key) |
|
35 | 35 | end |
|
36 | 36 | |
|
37 | 37 | # Returns true if the column is sortable, otherwise false |
|
38 | 38 | def sortable? |
|
39 | 39 | !@sortable.nil? |
|
40 | 40 | end |
|
41 | 41 | |
|
42 | 42 | def sortable |
|
43 | 43 | @sortable.is_a?(Proc) ? @sortable.call : @sortable |
|
44 | 44 | end |
|
45 | 45 | |
|
46 | 46 | def value(issue) |
|
47 | 47 | issue.send name |
|
48 | 48 | end |
|
49 | 49 | |
|
50 | 50 | def css_classes |
|
51 | 51 | name |
|
52 | 52 | end |
|
53 | 53 | end |
|
54 | 54 | |
|
55 | 55 | class QueryCustomFieldColumn < QueryColumn |
|
56 | 56 | |
|
57 | 57 | def initialize(custom_field) |
|
58 | 58 | self.name = "cf_#{custom_field.id}".to_sym |
|
59 | 59 | self.sortable = custom_field.order_statement || false |
|
60 | 60 | self.groupable = custom_field.group_statement || false |
|
61 | 61 | @cf = custom_field |
|
62 | 62 | end |
|
63 | 63 | |
|
64 | 64 | def caption |
|
65 | 65 | @cf.name |
|
66 | 66 | end |
|
67 | 67 | |
|
68 | 68 | def custom_field |
|
69 | 69 | @cf |
|
70 | 70 | end |
|
71 | 71 | |
|
72 | 72 | def value(issue) |
|
73 | 73 | cv = issue.custom_values.select {|v| v.custom_field_id == @cf.id}.collect {|v| @cf.cast_value(v.value)} |
|
74 | 74 | cv.size > 1 ? cv.sort {|a,b| a.to_s <=> b.to_s} : cv.first |
|
75 | 75 | end |
|
76 | 76 | |
|
77 | 77 | def css_classes |
|
78 | 78 | @css_classes ||= "#{name} #{@cf.field_format}" |
|
79 | 79 | end |
|
80 | 80 | end |
|
81 | 81 | |
|
82 | 82 | class Query < ActiveRecord::Base |
|
83 | 83 | class StatementInvalid < ::ActiveRecord::StatementInvalid |
|
84 | 84 | end |
|
85 | 85 | |
|
86 | 86 | belongs_to :project |
|
87 | 87 | belongs_to :user |
|
88 | 88 | serialize :filters |
|
89 | 89 | serialize :column_names |
|
90 | 90 | serialize :sort_criteria, Array |
|
91 | 91 | |
|
92 | 92 | attr_protected :project_id, :user_id |
|
93 | 93 | |
|
94 | 94 | validates_presence_of :name |
|
95 | 95 | validates_length_of :name, :maximum => 255 |
|
96 | 96 | validate :validate_query_filters |
|
97 | 97 | |
|
98 | 98 | @@operators = { "=" => :label_equals, |
|
99 | 99 | "!" => :label_not_equals, |
|
100 | 100 | "o" => :label_open_issues, |
|
101 | 101 | "c" => :label_closed_issues, |
|
102 | 102 | "!*" => :label_none, |
|
103 | 103 | "*" => :label_any, |
|
104 | 104 | ">=" => :label_greater_or_equal, |
|
105 | 105 | "<=" => :label_less_or_equal, |
|
106 | 106 | "><" => :label_between, |
|
107 | 107 | "<t+" => :label_in_less_than, |
|
108 | 108 | ">t+" => :label_in_more_than, |
|
109 | 109 | "t+" => :label_in, |
|
110 | 110 | "t" => :label_today, |
|
111 | 111 | "w" => :label_this_week, |
|
112 | 112 | ">t-" => :label_less_than_ago, |
|
113 | 113 | "<t-" => :label_more_than_ago, |
|
114 | 114 | "t-" => :label_ago, |
|
115 | 115 | "~" => :label_contains, |
|
116 | 116 | "!~" => :label_not_contains, |
|
117 | 117 | "=p" => :label_any_issues_in_project, |
|
118 | 118 | "=!p" => :label_any_issues_not_in_project, |
|
119 | 119 | "!p" => :label_no_issues_in_project} |
|
120 | 120 | |
|
121 | 121 | cattr_reader :operators |
|
122 | 122 | |
|
123 | 123 | @@operators_by_filter_type = { :list => [ "=", "!" ], |
|
124 | 124 | :list_status => [ "o", "=", "!", "c", "*" ], |
|
125 | 125 | :list_optional => [ "=", "!", "!*", "*" ], |
|
126 | 126 | :list_subprojects => [ "*", "!*", "=" ], |
|
127 | 127 | :date => [ "=", ">=", "<=", "><", "<t+", ">t+", "t+", "t", "w", ">t-", "<t-", "t-", "!*", "*" ], |
|
128 | 128 | :date_past => [ "=", ">=", "<=", "><", ">t-", "<t-", "t-", "t", "w", "!*", "*" ], |
|
129 | 129 | :string => [ "=", "~", "!", "!~", "!*", "*" ], |
|
130 | 130 | :text => [ "~", "!~", "!*", "*" ], |
|
131 | 131 | :integer => [ "=", ">=", "<=", "><", "!*", "*" ], |
|
132 | 132 | :float => [ "=", ">=", "<=", "><", "!*", "*" ], |
|
133 | 133 | :relation => ["=", "=p", "=!p", "!p", "!*", "*"]} |
|
134 | 134 | |
|
135 | 135 | cattr_reader :operators_by_filter_type |
|
136 | 136 | |
|
137 | 137 | @@available_columns = [ |
|
138 | 138 | QueryColumn.new(:project, :sortable => "#{Project.table_name}.name", :groupable => true), |
|
139 | 139 | QueryColumn.new(:tracker, :sortable => "#{Tracker.table_name}.position", :groupable => true), |
|
140 | 140 | QueryColumn.new(:parent, :sortable => ["#{Issue.table_name}.root_id", "#{Issue.table_name}.lft ASC"], :default_order => 'desc', :caption => :field_parent_issue), |
|
141 | 141 | QueryColumn.new(:status, :sortable => "#{IssueStatus.table_name}.position", :groupable => true), |
|
142 | 142 | QueryColumn.new(:priority, :sortable => "#{IssuePriority.table_name}.position", :default_order => 'desc', :groupable => true), |
|
143 | 143 | QueryColumn.new(:subject, :sortable => "#{Issue.table_name}.subject"), |
|
144 | 144 | QueryColumn.new(:author, :sortable => lambda {User.fields_for_order_statement("authors")}, :groupable => true), |
|
145 | 145 | QueryColumn.new(:assigned_to, :sortable => lambda {User.fields_for_order_statement}, :groupable => true), |
|
146 | 146 | QueryColumn.new(:updated_on, :sortable => "#{Issue.table_name}.updated_on", :default_order => 'desc'), |
|
147 | 147 | QueryColumn.new(:category, :sortable => "#{IssueCategory.table_name}.name", :groupable => true), |
|
148 | 148 | QueryColumn.new(:fixed_version, :sortable => lambda {Version.fields_for_order_statement}, :groupable => true), |
|
149 | 149 | QueryColumn.new(:start_date, :sortable => "#{Issue.table_name}.start_date"), |
|
150 | 150 | QueryColumn.new(:due_date, :sortable => "#{Issue.table_name}.due_date"), |
|
151 | 151 | QueryColumn.new(:estimated_hours, :sortable => "#{Issue.table_name}.estimated_hours"), |
|
152 | 152 | QueryColumn.new(:done_ratio, :sortable => "#{Issue.table_name}.done_ratio", :groupable => true), |
|
153 | 153 | QueryColumn.new(:created_on, :sortable => "#{Issue.table_name}.created_on", :default_order => 'desc'), |
|
154 | 154 | QueryColumn.new(:relations, :caption => :label_related_issues) |
|
155 | 155 | ] |
|
156 | 156 | cattr_reader :available_columns |
|
157 | 157 | |
|
158 | 158 | scope :visible, lambda {|*args| |
|
159 | 159 | user = args.shift || User.current |
|
160 | 160 | base = Project.allowed_to_condition(user, :view_issues, *args) |
|
161 | 161 | user_id = user.logged? ? user.id : 0 |
|
162 | 162 | { |
|
163 | 163 | :conditions => ["(#{table_name}.project_id IS NULL OR (#{base})) AND (#{table_name}.is_public = ? OR #{table_name}.user_id = ?)", true, user_id], |
|
164 | 164 | :include => :project |
|
165 | 165 | } |
|
166 | 166 | } |
|
167 | 167 | |
|
168 | 168 | def initialize(attributes=nil, *args) |
|
169 | 169 | super attributes |
|
170 | 170 | self.filters ||= { 'status_id' => {:operator => "o", :values => [""]} } |
|
171 | 171 | @is_for_all = project.nil? |
|
172 | 172 | end |
|
173 | 173 | |
|
174 | 174 | def validate_query_filters |
|
175 | 175 | filters.each_key do |field| |
|
176 | 176 | if values_for(field) |
|
177 | 177 | case type_for(field) |
|
178 | 178 | when :integer |
|
179 | 179 | add_filter_error(field, :invalid) if values_for(field).detect {|v| v.present? && !v.match(/^[+-]?\d+$/) } |
|
180 | 180 | when :float |
|
181 | 181 | add_filter_error(field, :invalid) if values_for(field).detect {|v| v.present? && !v.match(/^[+-]?\d+(\.\d*)?$/) } |
|
182 | 182 | when :date, :date_past |
|
183 | 183 | case operator_for(field) |
|
184 | 184 | when "=", ">=", "<=", "><" |
|
185 | 185 | add_filter_error(field, :invalid) if values_for(field).detect {|v| v.present? && (!v.match(/^\d{4}-\d{2}-\d{2}$/) || (Date.parse(v) rescue nil).nil?) } |
|
186 | 186 | when ">t-", "<t-", "t-" |
|
187 | 187 | add_filter_error(field, :invalid) if values_for(field).detect {|v| v.present? && !v.match(/^\d+$/) } |
|
188 | 188 | end |
|
189 | 189 | end |
|
190 | 190 | end |
|
191 | 191 | |
|
192 | 192 | add_filter_error(field, :blank) unless |
|
193 | 193 | # filter requires one or more values |
|
194 | 194 | (values_for(field) and !values_for(field).first.blank?) or |
|
195 | 195 | # filter doesn't require any value |
|
196 | 196 | ["o", "c", "!*", "*", "t", "w"].include? operator_for(field) |
|
197 | 197 | end if filters |
|
198 | 198 | end |
|
199 | 199 | |
|
200 | 200 | def add_filter_error(field, message) |
|
201 | 201 | m = label_for(field) + " " + l(message, :scope => 'activerecord.errors.messages') |
|
202 | 202 | errors.add(:base, m) |
|
203 | 203 | end |
|
204 | 204 | |
|
205 | 205 | # Returns true if the query is visible to +user+ or the current user. |
|
206 | 206 | def visible?(user=User.current) |
|
207 | 207 | (project.nil? || user.allowed_to?(:view_issues, project)) && (self.is_public? || self.user_id == user.id) |
|
208 | 208 | end |
|
209 | 209 | |
|
210 | 210 | def editable_by?(user) |
|
211 | 211 | return false unless user |
|
212 | 212 | # Admin can edit them all and regular users can edit their private queries |
|
213 | 213 | return true if user.admin? || (!is_public && self.user_id == user.id) |
|
214 | 214 | # Members can not edit public queries that are for all project (only admin is allowed to) |
|
215 | 215 | is_public && !@is_for_all && user.allowed_to?(:manage_public_queries, project) |
|
216 | 216 | end |
|
217 | 217 | |
|
218 | 218 | def trackers |
|
219 | 219 | @trackers ||= project.nil? ? Tracker.find(:all, :order => 'position') : project.rolled_up_trackers |
|
220 | 220 | end |
|
221 | 221 | |
|
222 | 222 | # Returns a hash of localized labels for all filter operators |
|
223 | 223 | def self.operators_labels |
|
224 | 224 | operators.inject({}) {|h, operator| h[operator.first] = l(operator.last); h} |
|
225 | 225 | end |
|
226 | 226 | |
|
227 | 227 | def available_filters |
|
228 | 228 | return @available_filters if @available_filters |
|
229 | 229 | @available_filters = { |
|
230 | 230 | "status_id" => { |
|
231 | 231 | :type => :list_status, :order => 0, |
|
232 | 232 | :values => IssueStatus.find(:all, :order => 'position').collect{|s| [s.name, s.id.to_s] } |
|
233 | 233 | }, |
|
234 | 234 | "tracker_id" => { |
|
235 | 235 | :type => :list, :order => 2, :values => trackers.collect{|s| [s.name, s.id.to_s] } |
|
236 | 236 | }, |
|
237 | 237 | "priority_id" => { |
|
238 | 238 | :type => :list, :order => 3, :values => IssuePriority.all.collect{|s| [s.name, s.id.to_s] } |
|
239 | 239 | }, |
|
240 | 240 | "subject" => { :type => :text, :order => 8 }, |
|
241 | 241 | "created_on" => { :type => :date_past, :order => 9 }, |
|
242 | 242 | "updated_on" => { :type => :date_past, :order => 10 }, |
|
243 | 243 | "start_date" => { :type => :date, :order => 11 }, |
|
244 | 244 | "due_date" => { :type => :date, :order => 12 }, |
|
245 | 245 | "estimated_hours" => { :type => :float, :order => 13 }, |
|
246 | 246 | "done_ratio" => { :type => :integer, :order => 14 } |
|
247 | 247 | } |
|
248 | 248 | IssueRelation::TYPES.each do |relation_type, options| |
|
249 | 249 | @available_filters[relation_type] = { |
|
250 | 250 | :type => :relation, :order => @available_filters.size + 100, |
|
251 | 251 | :label => options[:name] |
|
252 | 252 | } |
|
253 | 253 | end |
|
254 | 254 | principals = [] |
|
255 | 255 | if project |
|
256 | 256 | principals += project.principals.sort |
|
257 | 257 | unless project.leaf? |
|
258 | 258 | subprojects = project.descendants.visible.all |
|
259 | 259 | if subprojects.any? |
|
260 | 260 | @available_filters["subproject_id"] = { |
|
261 | 261 | :type => :list_subprojects, :order => 13, |
|
262 | 262 | :values => subprojects.collect{|s| [s.name, s.id.to_s] } |
|
263 | 263 | } |
|
264 | 264 | principals += Principal.member_of(subprojects) |
|
265 | 265 | end |
|
266 | 266 | end |
|
267 | 267 | else |
|
268 | 268 | if all_projects.any? |
|
269 | 269 | # members of visible projects |
|
270 | 270 | principals += Principal.member_of(all_projects) |
|
271 | 271 | # project filter |
|
272 | 272 | project_values = [] |
|
273 | 273 | if User.current.logged? && User.current.memberships.any? |
|
274 | 274 | project_values << ["<< #{l(:label_my_projects).downcase} >>", "mine"] |
|
275 | 275 | end |
|
276 | 276 | project_values += all_projects_values |
|
277 | 277 | @available_filters["project_id"] = { |
|
278 | 278 | :type => :list, :order => 1, :values => project_values |
|
279 | 279 | } unless project_values.empty? |
|
280 | 280 | end |
|
281 | 281 | end |
|
282 | 282 | principals.uniq! |
|
283 | 283 | principals.sort! |
|
284 | 284 | users = principals.select {|p| p.is_a?(User)} |
|
285 | 285 | |
|
286 | 286 | assigned_to_values = [] |
|
287 | 287 | assigned_to_values << ["<< #{l(:label_me)} >>", "me"] if User.current.logged? |
|
288 | 288 | assigned_to_values += (Setting.issue_group_assignment? ? |
|
289 | 289 | principals : users).collect{|s| [s.name, s.id.to_s] } |
|
290 | 290 | @available_filters["assigned_to_id"] = { |
|
291 | 291 | :type => :list_optional, :order => 4, :values => assigned_to_values |
|
292 | 292 | } unless assigned_to_values.empty? |
|
293 | 293 | |
|
294 | 294 | author_values = [] |
|
295 | 295 | author_values << ["<< #{l(:label_me)} >>", "me"] if User.current.logged? |
|
296 | 296 | author_values += users.collect{|s| [s.name, s.id.to_s] } |
|
297 | 297 | @available_filters["author_id"] = { |
|
298 | 298 | :type => :list, :order => 5, :values => author_values |
|
299 | 299 | } unless author_values.empty? |
|
300 | 300 | |
|
301 | 301 | group_values = Group.all.collect {|g| [g.name, g.id.to_s] } |
|
302 | 302 | @available_filters["member_of_group"] = { |
|
303 | 303 | :type => :list_optional, :order => 6, :values => group_values |
|
304 | 304 | } unless group_values.empty? |
|
305 | 305 | |
|
306 | 306 | role_values = Role.givable.collect {|r| [r.name, r.id.to_s] } |
|
307 | 307 | @available_filters["assigned_to_role"] = { |
|
308 | 308 | :type => :list_optional, :order => 7, :values => role_values |
|
309 | 309 | } unless role_values.empty? |
|
310 | 310 | |
|
311 | 311 | if User.current.logged? |
|
312 | 312 | @available_filters["watcher_id"] = { |
|
313 | 313 | :type => :list, :order => 15, :values => [["<< #{l(:label_me)} >>", "me"]] |
|
314 | 314 | } |
|
315 | 315 | end |
|
316 | 316 | |
|
317 | 317 | if project |
|
318 | 318 | # project specific filters |
|
319 | 319 | categories = project.issue_categories.all |
|
320 | 320 | unless categories.empty? |
|
321 | 321 | @available_filters["category_id"] = { |
|
322 | 322 | :type => :list_optional, :order => 6, |
|
323 | 323 | :values => categories.collect{|s| [s.name, s.id.to_s] } |
|
324 | 324 | } |
|
325 | 325 | end |
|
326 | 326 | versions = project.shared_versions.all |
|
327 | 327 | unless versions.empty? |
|
328 | 328 | @available_filters["fixed_version_id"] = { |
|
329 | 329 | :type => :list_optional, :order => 7, |
|
330 | 330 | :values => versions.sort.collect{|s| ["#{s.project.name} - #{s.name}", s.id.to_s] } |
|
331 | 331 | } |
|
332 | 332 | end |
|
333 | 333 | add_custom_fields_filters(project.all_issue_custom_fields) |
|
334 | 334 | else |
|
335 | 335 | # global filters for cross project issue list |
|
336 | 336 | system_shared_versions = Version.visible.find_all_by_sharing('system') |
|
337 | 337 | unless system_shared_versions.empty? |
|
338 | 338 | @available_filters["fixed_version_id"] = { |
|
339 | 339 | :type => :list_optional, :order => 7, |
|
340 | 340 | :values => system_shared_versions.sort.collect{|s| |
|
341 | 341 | ["#{s.project.name} - #{s.name}", s.id.to_s] |
|
342 | 342 | } |
|
343 | 343 | } |
|
344 | 344 | end |
|
345 | 345 | add_custom_fields_filters( |
|
346 | 346 | IssueCustomField.find(:all, |
|
347 | 347 | :conditions => { |
|
348 | 348 | :is_filter => true, |
|
349 | 349 | :is_for_all => true |
|
350 | 350 | })) |
|
351 | 351 | end |
|
352 | 352 | add_associations_custom_fields_filters :project, :author, :assigned_to, :fixed_version |
|
353 | 353 | if User.current.allowed_to?(:set_issues_private, nil, :global => true) || |
|
354 | 354 | User.current.allowed_to?(:set_own_issues_private, nil, :global => true) |
|
355 | 355 | @available_filters["is_private"] = { |
|
356 | 356 | :type => :list, :order => 16, |
|
357 | 357 | :values => [[l(:general_text_yes), "1"], [l(:general_text_no), "0"]] |
|
358 | 358 | } |
|
359 | 359 | end |
|
360 | 360 | Tracker.disabled_core_fields(trackers).each {|field| |
|
361 | 361 | @available_filters.delete field |
|
362 | 362 | } |
|
363 | 363 | @available_filters.each do |field, options| |
|
364 | 364 | options[:name] ||= l(options[:label] || "field_#{field}".gsub(/_id$/, '')) |
|
365 | 365 | end |
|
366 | 366 | @available_filters |
|
367 | 367 | end |
|
368 | 368 | |
|
369 | 369 | # Returns a representation of the available filters for JSON serialization |
|
370 | 370 | def available_filters_as_json |
|
371 | 371 | json = {} |
|
372 | 372 | available_filters.each do |field, options| |
|
373 | 373 | json[field] = options.slice(:type, :name, :values).stringify_keys |
|
374 | 374 | end |
|
375 | 375 | json |
|
376 | 376 | end |
|
377 | 377 | |
|
378 | 378 | def all_projects |
|
379 | 379 | @all_projects ||= Project.visible.all |
|
380 | 380 | end |
|
381 | 381 | |
|
382 | 382 | def all_projects_values |
|
383 | 383 | return @all_projects_values if @all_projects_values |
|
384 | 384 | |
|
385 | 385 | values = [] |
|
386 | 386 | Project.project_tree(all_projects) do |p, level| |
|
387 | 387 | prefix = (level > 0 ? ('--' * level + ' ') : '') |
|
388 | 388 | values << ["#{prefix}#{p.name}", p.id.to_s] |
|
389 | 389 | end |
|
390 | 390 | @all_projects_values = values |
|
391 | 391 | end |
|
392 | 392 | |
|
393 | 393 | def add_filter(field, operator, values) |
|
394 | 394 | # values must be an array |
|
395 | 395 | return unless values.nil? || values.is_a?(Array) |
|
396 | 396 | # check if field is defined as an available filter |
|
397 | 397 | if available_filters.has_key? field |
|
398 | 398 | filter_options = available_filters[field] |
|
399 | 399 | # check if operator is allowed for that filter |
|
400 | 400 | #if @@operators_by_filter_type[filter_options[:type]].include? operator |
|
401 | 401 | # allowed_values = values & ([""] + (filter_options[:values] || []).collect {|val| val[1]}) |
|
402 | 402 | # filters[field] = {:operator => operator, :values => allowed_values } if (allowed_values.first and !allowed_values.first.empty?) or ["o", "c", "!*", "*", "t"].include? operator |
|
403 | 403 | #end |
|
404 | 404 | filters[field] = {:operator => operator, :values => (values || [''])} |
|
405 | 405 | end |
|
406 | 406 | end |
|
407 | 407 | |
|
408 | 408 | def add_short_filter(field, expression) |
|
409 | 409 | return unless expression && available_filters.has_key?(field) |
|
410 | 410 | field_type = available_filters[field][:type] |
|
411 | 411 | @@operators_by_filter_type[field_type].sort.reverse.detect do |operator| |
|
412 | 412 | next unless expression =~ /^#{Regexp.escape(operator)}(.*)$/ |
|
413 | 413 | add_filter field, operator, $1.present? ? $1.split('|') : [''] |
|
414 | 414 | end || add_filter(field, '=', expression.split('|')) |
|
415 | 415 | end |
|
416 | 416 | |
|
417 | 417 | # Add multiple filters using +add_filter+ |
|
418 | 418 | def add_filters(fields, operators, values) |
|
419 | 419 | if fields.is_a?(Array) && operators.is_a?(Hash) && (values.nil? || values.is_a?(Hash)) |
|
420 | 420 | fields.each do |field| |
|
421 | 421 | add_filter(field, operators[field], values && values[field]) |
|
422 | 422 | end |
|
423 | 423 | end |
|
424 | 424 | end |
|
425 | 425 | |
|
426 | 426 | def has_filter?(field) |
|
427 | 427 | filters and filters[field] |
|
428 | 428 | end |
|
429 | 429 | |
|
430 | 430 | def type_for(field) |
|
431 | 431 | available_filters[field][:type] if available_filters.has_key?(field) |
|
432 | 432 | end |
|
433 | 433 | |
|
434 | 434 | def operator_for(field) |
|
435 | 435 | has_filter?(field) ? filters[field][:operator] : nil |
|
436 | 436 | end |
|
437 | 437 | |
|
438 | 438 | def values_for(field) |
|
439 | 439 | has_filter?(field) ? filters[field][:values] : nil |
|
440 | 440 | end |
|
441 | 441 | |
|
442 | 442 | def value_for(field, index=0) |
|
443 | 443 | (values_for(field) || [])[index] |
|
444 | 444 | end |
|
445 | 445 | |
|
446 | 446 | def label_for(field) |
|
447 | 447 | label = available_filters[field][:name] if available_filters.has_key?(field) |
|
448 | 448 | label ||= l("field_#{field.to_s.gsub(/_id$/, '')}", :default => field) |
|
449 | 449 | end |
|
450 | 450 | |
|
451 | 451 | def available_columns |
|
452 | 452 | return @available_columns if @available_columns |
|
453 | 453 | @available_columns = ::Query.available_columns.dup |
|
454 | 454 | @available_columns += (project ? |
|
455 | 455 | project.all_issue_custom_fields : |
|
456 | 456 | IssueCustomField.find(:all) |
|
457 | 457 | ).collect {|cf| QueryCustomFieldColumn.new(cf) } |
|
458 | 458 | |
|
459 | 459 | if User.current.allowed_to?(:view_time_entries, project, :global => true) |
|
460 | 460 | index = nil |
|
461 | 461 | @available_columns.each_with_index {|column, i| index = i if column.name == :estimated_hours} |
|
462 | 462 | index = (index ? index + 1 : -1) |
|
463 | 463 | # insert the column after estimated_hours or at the end |
|
464 | 464 | @available_columns.insert index, QueryColumn.new(:spent_hours, |
|
465 | 465 | :sortable => "(SELECT COALESCE(SUM(hours), 0) FROM #{TimeEntry.table_name} WHERE #{TimeEntry.table_name}.issue_id = #{Issue.table_name}.id)", |
|
466 | 466 | :default_order => 'desc', |
|
467 | 467 | :caption => :label_spent_time |
|
468 | 468 | ) |
|
469 | 469 | end |
|
470 | 470 | |
|
471 | 471 | if User.current.allowed_to?(:set_issues_private, nil, :global => true) || |
|
472 | 472 | User.current.allowed_to?(:set_own_issues_private, nil, :global => true) |
|
473 | 473 | @available_columns << QueryColumn.new(:is_private, :sortable => "#{Issue.table_name}.is_private") |
|
474 | 474 | end |
|
475 | 475 | |
|
476 | 476 | disabled_fields = Tracker.disabled_core_fields(trackers).map {|field| field.sub(/_id$/, '')} |
|
477 | 477 | @available_columns.reject! {|column| |
|
478 | 478 | disabled_fields.include?(column.name.to_s) |
|
479 | 479 | } |
|
480 | 480 | |
|
481 | 481 | @available_columns |
|
482 | 482 | end |
|
483 | 483 | |
|
484 | 484 | def self.available_columns=(v) |
|
485 | 485 | self.available_columns = (v) |
|
486 | 486 | end |
|
487 | 487 | |
|
488 | 488 | def self.add_available_column(column) |
|
489 | 489 | self.available_columns << (column) if column.is_a?(QueryColumn) |
|
490 | 490 | end |
|
491 | 491 | |
|
492 | 492 | # Returns an array of columns that can be used to group the results |
|
493 | 493 | def groupable_columns |
|
494 | 494 | available_columns.select {|c| c.groupable} |
|
495 | 495 | end |
|
496 | 496 | |
|
497 | 497 | # Returns a Hash of columns and the key for sorting |
|
498 | 498 | def sortable_columns |
|
499 | 499 | {'id' => "#{Issue.table_name}.id"}.merge(available_columns.inject({}) {|h, column| |
|
500 | 500 | h[column.name.to_s] = column.sortable |
|
501 | 501 | h |
|
502 | 502 | }) |
|
503 | 503 | end |
|
504 | 504 | |
|
505 | 505 | def columns |
|
506 | 506 | # preserve the column_names order |
|
507 | 507 | (has_default_columns? ? default_columns_names : column_names).collect do |name| |
|
508 | 508 | available_columns.find { |col| col.name == name } |
|
509 | 509 | end.compact |
|
510 | 510 | end |
|
511 | 511 | |
|
512 | 512 | def default_columns_names |
|
513 | 513 | @default_columns_names ||= begin |
|
514 | 514 | default_columns = Setting.issue_list_default_columns.map(&:to_sym) |
|
515 | 515 | |
|
516 | 516 | project.present? ? default_columns : [:project] | default_columns |
|
517 | 517 | end |
|
518 | 518 | end |
|
519 | 519 | |
|
520 | 520 | def column_names=(names) |
|
521 | 521 | if names |
|
522 | 522 | names = names.select {|n| n.is_a?(Symbol) || !n.blank? } |
|
523 | 523 | names = names.collect {|n| n.is_a?(Symbol) ? n : n.to_sym } |
|
524 | 524 | # Set column_names to nil if default columns |
|
525 | 525 | if names == default_columns_names |
|
526 | 526 | names = nil |
|
527 | 527 | end |
|
528 | 528 | end |
|
529 | 529 | write_attribute(:column_names, names) |
|
530 | 530 | end |
|
531 | 531 | |
|
532 | 532 | def has_column?(column) |
|
533 | 533 | column_names && column_names.include?(column.is_a?(QueryColumn) ? column.name : column) |
|
534 | 534 | end |
|
535 | 535 | |
|
536 | 536 | def has_default_columns? |
|
537 | 537 | column_names.nil? || column_names.empty? |
|
538 | 538 | end |
|
539 | 539 | |
|
540 | 540 | def sort_criteria=(arg) |
|
541 | 541 | c = [] |
|
542 | 542 | if arg.is_a?(Hash) |
|
543 | 543 | arg = arg.keys.sort.collect {|k| arg[k]} |
|
544 | 544 | end |
|
545 |
c = arg.select {|k,o| !k.to_s.blank?}.slice(0,3).collect {|k,o| [k.to_s, o == 'desc' ? |
|
|
545 | c = arg.select {|k,o| !k.to_s.blank?}.slice(0,3).collect {|k,o| [k.to_s, (o == 'desc' || o == false) ? 'desc' : 'asc']} | |
|
546 | 546 | write_attribute(:sort_criteria, c) |
|
547 | 547 | end |
|
548 | 548 | |
|
549 | 549 | def sort_criteria |
|
550 | 550 | read_attribute(:sort_criteria) || [] |
|
551 | 551 | end |
|
552 | 552 | |
|
553 | 553 | def sort_criteria_key(arg) |
|
554 | 554 | sort_criteria && sort_criteria[arg] && sort_criteria[arg].first |
|
555 | 555 | end |
|
556 | 556 | |
|
557 | 557 | def sort_criteria_order(arg) |
|
558 | 558 | sort_criteria && sort_criteria[arg] && sort_criteria[arg].last |
|
559 | 559 | end |
|
560 | 560 | |
|
561 | def sort_criteria_order_for(key) | |
|
562 | sort_criteria.detect {|k, order| key.to_s == k}.try(:last) | |
|
563 | end | |
|
564 | ||
|
561 | 565 | # Returns the SQL sort order that should be prepended for grouping |
|
562 | 566 | def group_by_sort_order |
|
563 | 567 | if grouped? && (column = group_by_column) |
|
568 | order = sort_criteria_order_for(column.name) || column.default_order | |
|
564 | 569 | column.sortable.is_a?(Array) ? |
|
565 |
column.sortable.collect {|s| "#{s} #{ |
|
|
566 |
"#{column.sortable} #{ |
|
|
570 | column.sortable.collect {|s| "#{s} #{order}"}.join(',') : | |
|
571 | "#{column.sortable} #{order}" | |
|
567 | 572 | end |
|
568 | 573 | end |
|
569 | 574 | |
|
570 | 575 | # Returns true if the query is a grouped query |
|
571 | 576 | def grouped? |
|
572 | 577 | !group_by_column.nil? |
|
573 | 578 | end |
|
574 | 579 | |
|
575 | 580 | def group_by_column |
|
576 | 581 | groupable_columns.detect {|c| c.groupable && c.name.to_s == group_by} |
|
577 | 582 | end |
|
578 | 583 | |
|
579 | 584 | def group_by_statement |
|
580 | 585 | group_by_column.try(:groupable) |
|
581 | 586 | end |
|
582 | 587 | |
|
583 | 588 | def project_statement |
|
584 | 589 | project_clauses = [] |
|
585 | 590 | if project && !project.descendants.active.empty? |
|
586 | 591 | ids = [project.id] |
|
587 | 592 | if has_filter?("subproject_id") |
|
588 | 593 | case operator_for("subproject_id") |
|
589 | 594 | when '=' |
|
590 | 595 | # include the selected subprojects |
|
591 | 596 | ids += values_for("subproject_id").each(&:to_i) |
|
592 | 597 | when '!*' |
|
593 | 598 | # main project only |
|
594 | 599 | else |
|
595 | 600 | # all subprojects |
|
596 | 601 | ids += project.descendants.collect(&:id) |
|
597 | 602 | end |
|
598 | 603 | elsif Setting.display_subprojects_issues? |
|
599 | 604 | ids += project.descendants.collect(&:id) |
|
600 | 605 | end |
|
601 | 606 | project_clauses << "#{Project.table_name}.id IN (%s)" % ids.join(',') |
|
602 | 607 | elsif project |
|
603 | 608 | project_clauses << "#{Project.table_name}.id = %d" % project.id |
|
604 | 609 | end |
|
605 | 610 | project_clauses.any? ? project_clauses.join(' AND ') : nil |
|
606 | 611 | end |
|
607 | 612 | |
|
608 | 613 | def statement |
|
609 | 614 | # filters clauses |
|
610 | 615 | filters_clauses = [] |
|
611 | 616 | filters.each_key do |field| |
|
612 | 617 | next if field == "subproject_id" |
|
613 | 618 | v = values_for(field).clone |
|
614 | 619 | next unless v and !v.empty? |
|
615 | 620 | operator = operator_for(field) |
|
616 | 621 | |
|
617 | 622 | # "me" value subsitution |
|
618 | 623 | if %w(assigned_to_id author_id watcher_id).include?(field) |
|
619 | 624 | if v.delete("me") |
|
620 | 625 | if User.current.logged? |
|
621 | 626 | v.push(User.current.id.to_s) |
|
622 | 627 | v += User.current.group_ids.map(&:to_s) if field == 'assigned_to_id' |
|
623 | 628 | else |
|
624 | 629 | v.push("0") |
|
625 | 630 | end |
|
626 | 631 | end |
|
627 | 632 | end |
|
628 | 633 | |
|
629 | 634 | if field == 'project_id' |
|
630 | 635 | if v.delete('mine') |
|
631 | 636 | v += User.current.memberships.map(&:project_id).map(&:to_s) |
|
632 | 637 | end |
|
633 | 638 | end |
|
634 | 639 | |
|
635 | 640 | if field =~ /cf_(\d+)$/ |
|
636 | 641 | # custom field |
|
637 | 642 | filters_clauses << sql_for_custom_field(field, operator, v, $1) |
|
638 | 643 | elsif respond_to?("sql_for_#{field}_field") |
|
639 | 644 | # specific statement |
|
640 | 645 | filters_clauses << send("sql_for_#{field}_field", field, operator, v) |
|
641 | 646 | else |
|
642 | 647 | # regular field |
|
643 | 648 | filters_clauses << '(' + sql_for_field(field, operator, v, Issue.table_name, field) + ')' |
|
644 | 649 | end |
|
645 | 650 | end if filters and valid? |
|
646 | 651 | |
|
647 | 652 | filters_clauses << project_statement |
|
648 | 653 | filters_clauses.reject!(&:blank?) |
|
649 | 654 | |
|
650 | 655 | filters_clauses.any? ? filters_clauses.join(' AND ') : nil |
|
651 | 656 | end |
|
652 | 657 | |
|
653 | 658 | # Returns the issue count |
|
654 | 659 | def issue_count |
|
655 | 660 | Issue.visible.count(:include => [:status, :project], :conditions => statement) |
|
656 | 661 | rescue ::ActiveRecord::StatementInvalid => e |
|
657 | 662 | raise StatementInvalid.new(e.message) |
|
658 | 663 | end |
|
659 | 664 | |
|
660 | 665 | # Returns the issue count by group or nil if query is not grouped |
|
661 | 666 | def issue_count_by_group |
|
662 | 667 | r = nil |
|
663 | 668 | if grouped? |
|
664 | 669 | begin |
|
665 | 670 | # Rails3 will raise an (unexpected) RecordNotFound if there's only a nil group value |
|
666 | 671 | r = Issue.visible.count(:group => group_by_statement, :include => [:status, :project], :conditions => statement) |
|
667 | 672 | rescue ActiveRecord::RecordNotFound |
|
668 | 673 | r = {nil => issue_count} |
|
669 | 674 | end |
|
670 | 675 | c = group_by_column |
|
671 | 676 | if c.is_a?(QueryCustomFieldColumn) |
|
672 | 677 | r = r.keys.inject({}) {|h, k| h[c.custom_field.cast_value(k)] = r[k]; h} |
|
673 | 678 | end |
|
674 | 679 | end |
|
675 | 680 | r |
|
676 | 681 | rescue ::ActiveRecord::StatementInvalid => e |
|
677 | 682 | raise StatementInvalid.new(e.message) |
|
678 | 683 | end |
|
679 | 684 | |
|
680 | 685 | # Returns the issues |
|
681 | 686 | # Valid options are :order, :offset, :limit, :include, :conditions |
|
682 | 687 | def issues(options={}) |
|
683 | 688 | order_option = [group_by_sort_order, options[:order]].reject {|s| s.blank?}.join(',') |
|
684 | 689 | order_option = nil if order_option.blank? |
|
685 | 690 | |
|
686 | 691 | issues = Issue.visible.scoped(:conditions => options[:conditions]).find :all, :include => ([:status, :project] + (options[:include] || [])).uniq, |
|
687 | 692 | :conditions => statement, |
|
688 | 693 | :order => order_option, |
|
689 | 694 | :joins => joins_for_order_statement(order_option), |
|
690 | 695 | :limit => options[:limit], |
|
691 | 696 | :offset => options[:offset] |
|
692 | 697 | |
|
693 | 698 | if has_column?(:spent_hours) |
|
694 | 699 | Issue.load_visible_spent_hours(issues) |
|
695 | 700 | end |
|
696 | 701 | if has_column?(:relations) |
|
697 | 702 | Issue.load_visible_relations(issues) |
|
698 | 703 | end |
|
699 | 704 | issues |
|
700 | 705 | rescue ::ActiveRecord::StatementInvalid => e |
|
701 | 706 | raise StatementInvalid.new(e.message) |
|
702 | 707 | end |
|
703 | 708 | |
|
704 | 709 | # Returns the issues ids |
|
705 | 710 | def issue_ids(options={}) |
|
706 | 711 | order_option = [group_by_sort_order, options[:order]].reject {|s| s.blank?}.join(',') |
|
707 | 712 | order_option = nil if order_option.blank? |
|
708 | 713 | |
|
709 | 714 | Issue.visible.scoped(:conditions => options[:conditions]).scoped(:include => ([:status, :project] + (options[:include] || [])).uniq, |
|
710 | 715 | :conditions => statement, |
|
711 | 716 | :order => order_option, |
|
712 | 717 | :joins => joins_for_order_statement(order_option), |
|
713 | 718 | :limit => options[:limit], |
|
714 | 719 | :offset => options[:offset]).find_ids |
|
715 | 720 | rescue ::ActiveRecord::StatementInvalid => e |
|
716 | 721 | raise StatementInvalid.new(e.message) |
|
717 | 722 | end |
|
718 | 723 | |
|
719 | 724 | # Returns the journals |
|
720 | 725 | # Valid options are :order, :offset, :limit |
|
721 | 726 | def journals(options={}) |
|
722 | 727 | Journal.visible.find :all, :include => [:details, :user, {:issue => [:project, :author, :tracker, :status]}], |
|
723 | 728 | :conditions => statement, |
|
724 | 729 | :order => options[:order], |
|
725 | 730 | :limit => options[:limit], |
|
726 | 731 | :offset => options[:offset] |
|
727 | 732 | rescue ::ActiveRecord::StatementInvalid => e |
|
728 | 733 | raise StatementInvalid.new(e.message) |
|
729 | 734 | end |
|
730 | 735 | |
|
731 | 736 | # Returns the versions |
|
732 | 737 | # Valid options are :conditions |
|
733 | 738 | def versions(options={}) |
|
734 | 739 | Version.visible.scoped(:conditions => options[:conditions]).find :all, :include => :project, :conditions => project_statement |
|
735 | 740 | rescue ::ActiveRecord::StatementInvalid => e |
|
736 | 741 | raise StatementInvalid.new(e.message) |
|
737 | 742 | end |
|
738 | 743 | |
|
739 | 744 | def sql_for_watcher_id_field(field, operator, value) |
|
740 | 745 | db_table = Watcher.table_name |
|
741 | 746 | "#{Issue.table_name}.id #{ operator == '=' ? 'IN' : 'NOT IN' } (SELECT #{db_table}.watchable_id FROM #{db_table} WHERE #{db_table}.watchable_type='Issue' AND " + |
|
742 | 747 | sql_for_field(field, '=', value, db_table, 'user_id') + ')' |
|
743 | 748 | end |
|
744 | 749 | |
|
745 | 750 | def sql_for_member_of_group_field(field, operator, value) |
|
746 | 751 | if operator == '*' # Any group |
|
747 | 752 | groups = Group.all |
|
748 | 753 | operator = '=' # Override the operator since we want to find by assigned_to |
|
749 | 754 | elsif operator == "!*" |
|
750 | 755 | groups = Group.all |
|
751 | 756 | operator = '!' # Override the operator since we want to find by assigned_to |
|
752 | 757 | else |
|
753 | 758 | groups = Group.find_all_by_id(value) |
|
754 | 759 | end |
|
755 | 760 | groups ||= [] |
|
756 | 761 | |
|
757 | 762 | members_of_groups = groups.inject([]) {|user_ids, group| |
|
758 | 763 | if group && group.user_ids.present? |
|
759 | 764 | user_ids << group.user_ids |
|
760 | 765 | end |
|
761 | 766 | user_ids.flatten.uniq.compact |
|
762 | 767 | }.sort.collect(&:to_s) |
|
763 | 768 | |
|
764 | 769 | '(' + sql_for_field("assigned_to_id", operator, members_of_groups, Issue.table_name, "assigned_to_id", false) + ')' |
|
765 | 770 | end |
|
766 | 771 | |
|
767 | 772 | def sql_for_assigned_to_role_field(field, operator, value) |
|
768 | 773 | case operator |
|
769 | 774 | when "*", "!*" # Member / Not member |
|
770 | 775 | sw = operator == "!*" ? 'NOT' : '' |
|
771 | 776 | nl = operator == "!*" ? "#{Issue.table_name}.assigned_to_id IS NULL OR" : '' |
|
772 | 777 | "(#{nl} #{Issue.table_name}.assigned_to_id #{sw} IN (SELECT DISTINCT #{Member.table_name}.user_id FROM #{Member.table_name}" + |
|
773 | 778 | " WHERE #{Member.table_name}.project_id = #{Issue.table_name}.project_id))" |
|
774 | 779 | when "=", "!" |
|
775 | 780 | role_cond = value.any? ? |
|
776 | 781 | "#{MemberRole.table_name}.role_id IN (" + value.collect{|val| "'#{connection.quote_string(val)}'"}.join(",") + ")" : |
|
777 | 782 | "1=0" |
|
778 | 783 | |
|
779 | 784 | sw = operator == "!" ? 'NOT' : '' |
|
780 | 785 | nl = operator == "!" ? "#{Issue.table_name}.assigned_to_id IS NULL OR" : '' |
|
781 | 786 | "(#{nl} #{Issue.table_name}.assigned_to_id #{sw} IN (SELECT DISTINCT #{Member.table_name}.user_id FROM #{Member.table_name}, #{MemberRole.table_name}" + |
|
782 | 787 | " WHERE #{Member.table_name}.project_id = #{Issue.table_name}.project_id AND #{Member.table_name}.id = #{MemberRole.table_name}.member_id AND #{role_cond}))" |
|
783 | 788 | end |
|
784 | 789 | end |
|
785 | 790 | |
|
786 | 791 | def sql_for_is_private_field(field, operator, value) |
|
787 | 792 | op = (operator == "=" ? 'IN' : 'NOT IN') |
|
788 | 793 | va = value.map {|v| v == '0' ? connection.quoted_false : connection.quoted_true}.uniq.join(',') |
|
789 | 794 | |
|
790 | 795 | "#{Issue.table_name}.is_private #{op} (#{va})" |
|
791 | 796 | end |
|
792 | 797 | |
|
793 | 798 | def sql_for_relations(field, operator, value, options={}) |
|
794 | 799 | relation_options = IssueRelation::TYPES[field] |
|
795 | 800 | return relation_options unless relation_options |
|
796 | 801 | |
|
797 | 802 | relation_type = field |
|
798 | 803 | join_column, target_join_column = "issue_from_id", "issue_to_id" |
|
799 | 804 | if relation_options[:reverse] || options[:reverse] |
|
800 | 805 | relation_type = relation_options[:reverse] || relation_type |
|
801 | 806 | join_column, target_join_column = target_join_column, join_column |
|
802 | 807 | end |
|
803 | 808 | |
|
804 | 809 | sql = case operator |
|
805 | 810 | when "*", "!*" |
|
806 | 811 | op = (operator == "*" ? 'IN' : 'NOT IN') |
|
807 | 812 | "#{Issue.table_name}.id #{op} (SELECT DISTINCT #{IssueRelation.table_name}.#{join_column} FROM #{IssueRelation.table_name} WHERE #{IssueRelation.table_name}.relation_type = '#{connection.quote_string(relation_type)}')" |
|
808 | 813 | when "=", "!" |
|
809 | 814 | op = (operator == "=" ? 'IN' : 'NOT IN') |
|
810 | 815 | "#{Issue.table_name}.id #{op} (SELECT DISTINCT #{IssueRelation.table_name}.#{join_column} FROM #{IssueRelation.table_name} WHERE #{IssueRelation.table_name}.relation_type = '#{connection.quote_string(relation_type)}' AND #{IssueRelation.table_name}.#{target_join_column} = #{value.first.to_i})" |
|
811 | 816 | when "=p", "=!p", "!p" |
|
812 | 817 | op = (operator == "!p" ? 'NOT IN' : 'IN') |
|
813 | 818 | comp = (operator == "=!p" ? '<>' : '=') |
|
814 | 819 | "#{Issue.table_name}.id #{op} (SELECT DISTINCT #{IssueRelation.table_name}.#{join_column} FROM #{IssueRelation.table_name}, #{Issue.table_name} relissues WHERE #{IssueRelation.table_name}.relation_type = '#{connection.quote_string(relation_type)}' AND #{IssueRelation.table_name}.#{target_join_column} = relissues.id AND relissues.project_id #{comp} #{value.first.to_i})" |
|
815 | 820 | end |
|
816 | 821 | |
|
817 | 822 | if relation_options[:sym] == field && !options[:reverse] |
|
818 | 823 | sqls = [sql, sql_for_relations(field, operator, value, :reverse => true)] |
|
819 | 824 | sqls.join(["!", "!*", "!p"].include?(operator) ? " AND " : " OR ") |
|
820 | 825 | else |
|
821 | 826 | sql |
|
822 | 827 | end |
|
823 | 828 | end |
|
824 | 829 | |
|
825 | 830 | IssueRelation::TYPES.keys.each do |relation_type| |
|
826 | 831 | alias_method "sql_for_#{relation_type}_field".to_sym, :sql_for_relations |
|
827 | 832 | end |
|
828 | 833 | |
|
829 | 834 | private |
|
830 | 835 | |
|
831 | 836 | def sql_for_custom_field(field, operator, value, custom_field_id) |
|
832 | 837 | db_table = CustomValue.table_name |
|
833 | 838 | db_field = 'value' |
|
834 | 839 | filter = @available_filters[field] |
|
835 | 840 | return nil unless filter |
|
836 | 841 | if filter[:format] == 'user' |
|
837 | 842 | if value.delete('me') |
|
838 | 843 | value.push User.current.id.to_s |
|
839 | 844 | end |
|
840 | 845 | end |
|
841 | 846 | not_in = nil |
|
842 | 847 | if operator == '!' |
|
843 | 848 | # Makes ! operator work for custom fields with multiple values |
|
844 | 849 | operator = '=' |
|
845 | 850 | not_in = 'NOT' |
|
846 | 851 | end |
|
847 | 852 | customized_key = "id" |
|
848 | 853 | customized_class = Issue |
|
849 | 854 | if field =~ /^(.+)\.cf_/ |
|
850 | 855 | assoc = $1 |
|
851 | 856 | customized_key = "#{assoc}_id" |
|
852 | 857 | customized_class = Issue.reflect_on_association(assoc.to_sym).klass.base_class rescue nil |
|
853 | 858 | raise "Unknown Issue association #{assoc}" unless customized_class |
|
854 | 859 | end |
|
855 | 860 | "#{Issue.table_name}.#{customized_key} #{not_in} IN (SELECT #{customized_class.table_name}.id FROM #{customized_class.table_name} LEFT OUTER JOIN #{db_table} ON #{db_table}.customized_type='#{customized_class}' AND #{db_table}.customized_id=#{customized_class.table_name}.id AND #{db_table}.custom_field_id=#{custom_field_id} WHERE " + |
|
856 | 861 | sql_for_field(field, operator, value, db_table, db_field, true) + ')' |
|
857 | 862 | end |
|
858 | 863 | |
|
859 | 864 | # Helper method to generate the WHERE sql for a +field+, +operator+ and a +value+ |
|
860 | 865 | def sql_for_field(field, operator, value, db_table, db_field, is_custom_filter=false) |
|
861 | 866 | sql = '' |
|
862 | 867 | case operator |
|
863 | 868 | when "=" |
|
864 | 869 | if value.any? |
|
865 | 870 | case type_for(field) |
|
866 | 871 | when :date, :date_past |
|
867 | 872 | sql = date_clause(db_table, db_field, (Date.parse(value.first) rescue nil), (Date.parse(value.first) rescue nil)) |
|
868 | 873 | when :integer |
|
869 | 874 | if is_custom_filter |
|
870 | 875 | sql = "(#{db_table}.#{db_field} <> '' AND CAST(#{db_table}.#{db_field} AS decimal(60,3)) = #{value.first.to_i})" |
|
871 | 876 | else |
|
872 | 877 | sql = "#{db_table}.#{db_field} = #{value.first.to_i}" |
|
873 | 878 | end |
|
874 | 879 | when :float |
|
875 | 880 | if is_custom_filter |
|
876 | 881 | sql = "(#{db_table}.#{db_field} <> '' AND CAST(#{db_table}.#{db_field} AS decimal(60,3)) BETWEEN #{value.first.to_f - 1e-5} AND #{value.first.to_f + 1e-5})" |
|
877 | 882 | else |
|
878 | 883 | sql = "#{db_table}.#{db_field} BETWEEN #{value.first.to_f - 1e-5} AND #{value.first.to_f + 1e-5}" |
|
879 | 884 | end |
|
880 | 885 | else |
|
881 | 886 | sql = "#{db_table}.#{db_field} IN (" + value.collect{|val| "'#{connection.quote_string(val)}'"}.join(",") + ")" |
|
882 | 887 | end |
|
883 | 888 | else |
|
884 | 889 | # IN an empty set |
|
885 | 890 | sql = "1=0" |
|
886 | 891 | end |
|
887 | 892 | when "!" |
|
888 | 893 | if value.any? |
|
889 | 894 | sql = "(#{db_table}.#{db_field} IS NULL OR #{db_table}.#{db_field} NOT IN (" + value.collect{|val| "'#{connection.quote_string(val)}'"}.join(",") + "))" |
|
890 | 895 | else |
|
891 | 896 | # NOT IN an empty set |
|
892 | 897 | sql = "1=1" |
|
893 | 898 | end |
|
894 | 899 | when "!*" |
|
895 | 900 | sql = "#{db_table}.#{db_field} IS NULL" |
|
896 | 901 | sql << " OR #{db_table}.#{db_field} = ''" if is_custom_filter |
|
897 | 902 | when "*" |
|
898 | 903 | sql = "#{db_table}.#{db_field} IS NOT NULL" |
|
899 | 904 | sql << " AND #{db_table}.#{db_field} <> ''" if is_custom_filter |
|
900 | 905 | when ">=" |
|
901 | 906 | if [:date, :date_past].include?(type_for(field)) |
|
902 | 907 | sql = date_clause(db_table, db_field, (Date.parse(value.first) rescue nil), nil) |
|
903 | 908 | else |
|
904 | 909 | if is_custom_filter |
|
905 | 910 | sql = "(#{db_table}.#{db_field} <> '' AND CAST(#{db_table}.#{db_field} AS decimal(60,3)) >= #{value.first.to_f})" |
|
906 | 911 | else |
|
907 | 912 | sql = "#{db_table}.#{db_field} >= #{value.first.to_f}" |
|
908 | 913 | end |
|
909 | 914 | end |
|
910 | 915 | when "<=" |
|
911 | 916 | if [:date, :date_past].include?(type_for(field)) |
|
912 | 917 | sql = date_clause(db_table, db_field, nil, (Date.parse(value.first) rescue nil)) |
|
913 | 918 | else |
|
914 | 919 | if is_custom_filter |
|
915 | 920 | sql = "(#{db_table}.#{db_field} <> '' AND CAST(#{db_table}.#{db_field} AS decimal(60,3)) <= #{value.first.to_f})" |
|
916 | 921 | else |
|
917 | 922 | sql = "#{db_table}.#{db_field} <= #{value.first.to_f}" |
|
918 | 923 | end |
|
919 | 924 | end |
|
920 | 925 | when "><" |
|
921 | 926 | if [:date, :date_past].include?(type_for(field)) |
|
922 | 927 | sql = date_clause(db_table, db_field, (Date.parse(value[0]) rescue nil), (Date.parse(value[1]) rescue nil)) |
|
923 | 928 | else |
|
924 | 929 | if is_custom_filter |
|
925 | 930 | sql = "(#{db_table}.#{db_field} <> '' AND CAST(#{db_table}.#{db_field} AS decimal(60,3)) BETWEEN #{value[0].to_f} AND #{value[1].to_f})" |
|
926 | 931 | else |
|
927 | 932 | sql = "#{db_table}.#{db_field} BETWEEN #{value[0].to_f} AND #{value[1].to_f}" |
|
928 | 933 | end |
|
929 | 934 | end |
|
930 | 935 | when "o" |
|
931 | 936 | sql = "#{Issue.table_name}.status_id IN (SELECT id FROM #{IssueStatus.table_name} WHERE is_closed=#{connection.quoted_false})" if field == "status_id" |
|
932 | 937 | when "c" |
|
933 | 938 | sql = "#{Issue.table_name}.status_id IN (SELECT id FROM #{IssueStatus.table_name} WHERE is_closed=#{connection.quoted_true})" if field == "status_id" |
|
934 | 939 | when ">t-" |
|
935 | 940 | sql = relative_date_clause(db_table, db_field, - value.first.to_i, 0) |
|
936 | 941 | when "<t-" |
|
937 | 942 | sql = relative_date_clause(db_table, db_field, nil, - value.first.to_i) |
|
938 | 943 | when "t-" |
|
939 | 944 | sql = relative_date_clause(db_table, db_field, - value.first.to_i, - value.first.to_i) |
|
940 | 945 | when ">t+" |
|
941 | 946 | sql = relative_date_clause(db_table, db_field, value.first.to_i, nil) |
|
942 | 947 | when "<t+" |
|
943 | 948 | sql = relative_date_clause(db_table, db_field, 0, value.first.to_i) |
|
944 | 949 | when "t+" |
|
945 | 950 | sql = relative_date_clause(db_table, db_field, value.first.to_i, value.first.to_i) |
|
946 | 951 | when "t" |
|
947 | 952 | sql = relative_date_clause(db_table, db_field, 0, 0) |
|
948 | 953 | when "w" |
|
949 | 954 | first_day_of_week = l(:general_first_day_of_week).to_i |
|
950 | 955 | day_of_week = Date.today.cwday |
|
951 | 956 | days_ago = (day_of_week >= first_day_of_week ? day_of_week - first_day_of_week : day_of_week + 7 - first_day_of_week) |
|
952 | 957 | sql = relative_date_clause(db_table, db_field, - days_ago, - days_ago + 6) |
|
953 | 958 | when "~" |
|
954 | 959 | sql = "LOWER(#{db_table}.#{db_field}) LIKE '%#{connection.quote_string(value.first.to_s.downcase)}%'" |
|
955 | 960 | when "!~" |
|
956 | 961 | sql = "LOWER(#{db_table}.#{db_field}) NOT LIKE '%#{connection.quote_string(value.first.to_s.downcase)}%'" |
|
957 | 962 | else |
|
958 | 963 | raise "Unknown query operator #{operator}" |
|
959 | 964 | end |
|
960 | 965 | |
|
961 | 966 | return sql |
|
962 | 967 | end |
|
963 | 968 | |
|
964 | 969 | def add_custom_fields_filters(custom_fields, assoc=nil) |
|
965 | 970 | return unless custom_fields.present? |
|
966 | 971 | @available_filters ||= {} |
|
967 | 972 | |
|
968 | 973 | custom_fields.select(&:is_filter?).each do |field| |
|
969 | 974 | case field.field_format |
|
970 | 975 | when "text" |
|
971 | 976 | options = { :type => :text, :order => 20 } |
|
972 | 977 | when "list" |
|
973 | 978 | options = { :type => :list_optional, :values => field.possible_values, :order => 20} |
|
974 | 979 | when "date" |
|
975 | 980 | options = { :type => :date, :order => 20 } |
|
976 | 981 | when "bool" |
|
977 | 982 | options = { :type => :list, :values => [[l(:general_text_yes), "1"], [l(:general_text_no), "0"]], :order => 20 } |
|
978 | 983 | when "int" |
|
979 | 984 | options = { :type => :integer, :order => 20 } |
|
980 | 985 | when "float" |
|
981 | 986 | options = { :type => :float, :order => 20 } |
|
982 | 987 | when "user", "version" |
|
983 | 988 | next unless project |
|
984 | 989 | values = field.possible_values_options(project) |
|
985 | 990 | if User.current.logged? && field.field_format == 'user' |
|
986 | 991 | values.unshift ["<< #{l(:label_me)} >>", "me"] |
|
987 | 992 | end |
|
988 | 993 | options = { :type => :list_optional, :values => values, :order => 20} |
|
989 | 994 | else |
|
990 | 995 | options = { :type => :string, :order => 20 } |
|
991 | 996 | end |
|
992 | 997 | filter_id = "cf_#{field.id}" |
|
993 | 998 | filter_name = field.name |
|
994 | 999 | if assoc.present? |
|
995 | 1000 | filter_id = "#{assoc}.#{filter_id}" |
|
996 | 1001 | filter_name = l("label_attribute_of_#{assoc}", :name => filter_name) |
|
997 | 1002 | end |
|
998 | 1003 | @available_filters[filter_id] = options.merge({ |
|
999 | 1004 | :name => filter_name, |
|
1000 | 1005 | :format => field.field_format, |
|
1001 | 1006 | :field => field |
|
1002 | 1007 | }) |
|
1003 | 1008 | end |
|
1004 | 1009 | end |
|
1005 | 1010 | |
|
1006 | 1011 | def add_associations_custom_fields_filters(*associations) |
|
1007 | 1012 | fields_by_class = CustomField.where(:is_filter => true).group_by(&:class) |
|
1008 | 1013 | associations.each do |assoc| |
|
1009 | 1014 | association_klass = Issue.reflect_on_association(assoc).klass |
|
1010 | 1015 | fields_by_class.each do |field_class, fields| |
|
1011 | 1016 | if field_class.customized_class <= association_klass |
|
1012 | 1017 | add_custom_fields_filters(fields, assoc) |
|
1013 | 1018 | end |
|
1014 | 1019 | end |
|
1015 | 1020 | end |
|
1016 | 1021 | end |
|
1017 | 1022 | |
|
1018 | 1023 | # Returns a SQL clause for a date or datetime field. |
|
1019 | 1024 | def date_clause(table, field, from, to) |
|
1020 | 1025 | s = [] |
|
1021 | 1026 | if from |
|
1022 | 1027 | from_yesterday = from - 1 |
|
1023 | 1028 | from_yesterday_time = Time.local(from_yesterday.year, from_yesterday.month, from_yesterday.day) |
|
1024 | 1029 | if self.class.default_timezone == :utc |
|
1025 | 1030 | from_yesterday_time = from_yesterday_time.utc |
|
1026 | 1031 | end |
|
1027 | 1032 | s << ("#{table}.#{field} > '%s'" % [connection.quoted_date(from_yesterday_time.end_of_day)]) |
|
1028 | 1033 | end |
|
1029 | 1034 | if to |
|
1030 | 1035 | to_time = Time.local(to.year, to.month, to.day) |
|
1031 | 1036 | if self.class.default_timezone == :utc |
|
1032 | 1037 | to_time = to_time.utc |
|
1033 | 1038 | end |
|
1034 | 1039 | s << ("#{table}.#{field} <= '%s'" % [connection.quoted_date(to_time.end_of_day)]) |
|
1035 | 1040 | end |
|
1036 | 1041 | s.join(' AND ') |
|
1037 | 1042 | end |
|
1038 | 1043 | |
|
1039 | 1044 | # Returns a SQL clause for a date or datetime field using relative dates. |
|
1040 | 1045 | def relative_date_clause(table, field, days_from, days_to) |
|
1041 | 1046 | date_clause(table, field, (days_from ? Date.today + days_from : nil), (days_to ? Date.today + days_to : nil)) |
|
1042 | 1047 | end |
|
1043 | 1048 | |
|
1044 | 1049 | # Additional joins required for the given sort options |
|
1045 | 1050 | def joins_for_order_statement(order_options) |
|
1046 | 1051 | joins = [] |
|
1047 | 1052 | |
|
1048 | 1053 | if order_options |
|
1049 | 1054 | if order_options.include?('authors') |
|
1050 | 1055 | joins << "LEFT OUTER JOIN #{User.table_name} authors ON authors.id = #{Issue.table_name}.author_id" |
|
1051 | 1056 | end |
|
1052 | 1057 | order_options.scan(/cf_\d+/).uniq.each do |name| |
|
1053 | 1058 | column = available_columns.detect {|c| c.name.to_s == name} |
|
1054 | 1059 | join = column && column.custom_field.join_for_order_statement |
|
1055 | 1060 | if join |
|
1056 | 1061 | joins << join |
|
1057 | 1062 | end |
|
1058 | 1063 | end |
|
1059 | 1064 | end |
|
1060 | 1065 | |
|
1061 | 1066 | joins.any? ? joins.join(' ') : nil |
|
1062 | 1067 | end |
|
1063 | 1068 | end |
|
1 | NO CONTENT: modified file | |
The requested commit or file is too big and content was truncated. Show full diff |
General Comments 0
You need to be logged in to leave comments.
Login now