##// END OF EJS Templates
Changed filter position (#17720)....
Jean-Philippe Lang -
r15866:1eee6b3a30df
parent child
Show More
@@ -1,575 +1,575
1 1 # Redmine - project management software
2 2 # Copyright (C) 2006-2016 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 IssueQuery < Query
19 19
20 20 self.queried_class = Issue
21 21 self.view_permission = :view_issues
22 22
23 23 self.available_columns = [
24 24 QueryColumn.new(:id, :sortable => "#{Issue.table_name}.id", :default_order => 'desc', :caption => '#', :frozen => true),
25 25 QueryColumn.new(:project, :sortable => "#{Project.table_name}.name", :groupable => true),
26 26 QueryColumn.new(:tracker, :sortable => "#{Tracker.table_name}.position", :groupable => true),
27 27 QueryColumn.new(:parent, :sortable => ["#{Issue.table_name}.root_id", "#{Issue.table_name}.lft ASC"], :default_order => 'desc', :caption => :field_parent_issue),
28 28 QueryColumn.new(:status, :sortable => "#{IssueStatus.table_name}.position", :groupable => true),
29 29 QueryColumn.new(:priority, :sortable => "#{IssuePriority.table_name}.position", :default_order => 'desc', :groupable => true),
30 30 QueryColumn.new(:subject, :sortable => "#{Issue.table_name}.subject"),
31 31 QueryColumn.new(:author, :sortable => lambda {User.fields_for_order_statement("authors")}, :groupable => true),
32 32 QueryColumn.new(:assigned_to, :sortable => lambda {User.fields_for_order_statement}, :groupable => true),
33 33 QueryColumn.new(:updated_on, :sortable => "#{Issue.table_name}.updated_on", :default_order => 'desc'),
34 34 QueryColumn.new(:category, :sortable => "#{IssueCategory.table_name}.name", :groupable => true),
35 35 QueryColumn.new(:fixed_version, :sortable => lambda {Version.fields_for_order_statement}, :groupable => true),
36 36 QueryColumn.new(:start_date, :sortable => "#{Issue.table_name}.start_date"),
37 37 QueryColumn.new(:due_date, :sortable => "#{Issue.table_name}.due_date"),
38 38 QueryColumn.new(:estimated_hours, :sortable => "#{Issue.table_name}.estimated_hours", :totalable => true),
39 39 QueryColumn.new(:total_estimated_hours,
40 40 :sortable => "COALESCE((SELECT SUM(estimated_hours) FROM #{Issue.table_name} subtasks" +
41 41 " WHERE subtasks.root_id = #{Issue.table_name}.root_id AND subtasks.lft >= #{Issue.table_name}.lft AND subtasks.rgt <= #{Issue.table_name}.rgt), 0)",
42 42 :default_order => 'desc'),
43 43 QueryColumn.new(:done_ratio, :sortable => "#{Issue.table_name}.done_ratio", :groupable => true),
44 44 QueryColumn.new(:created_on, :sortable => "#{Issue.table_name}.created_on", :default_order => 'desc'),
45 45 QueryColumn.new(:closed_on, :sortable => "#{Issue.table_name}.closed_on", :default_order => 'desc'),
46 46 QueryColumn.new(:relations, :caption => :label_related_issues),
47 47 QueryColumn.new(:description, :inline => false)
48 48 ]
49 49
50 50 def initialize(attributes=nil, *args)
51 51 super attributes
52 52 self.filters ||= { 'status_id' => {:operator => "o", :values => [""]} }
53 53 end
54 54
55 55 def draw_relations
56 56 r = options[:draw_relations]
57 57 r.nil? || r == '1'
58 58 end
59 59
60 60 def draw_relations=(arg)
61 61 options[:draw_relations] = (arg == '0' ? '0' : nil)
62 62 end
63 63
64 64 def draw_progress_line
65 65 r = options[:draw_progress_line]
66 66 r == '1'
67 67 end
68 68
69 69 def draw_progress_line=(arg)
70 70 options[:draw_progress_line] = (arg == '1' ? '1' : nil)
71 71 end
72 72
73 73 def build_from_params(params)
74 74 super
75 75 self.draw_relations = params[:draw_relations] || (params[:query] && params[:query][:draw_relations])
76 76 self.draw_progress_line = params[:draw_progress_line] || (params[:query] && params[:query][:draw_progress_line])
77 77 self
78 78 end
79 79
80 80 def initialize_available_filters
81 81 add_available_filter "status_id",
82 82 :type => :list_status, :values => lambda { IssueStatus.sorted.collect{|s| [s.name, s.id.to_s] } }
83 83
84 84 add_available_filter("project_id",
85 85 :type => :list, :values => lambda { project_values }
86 86 ) if project.nil?
87 87
88 88 add_available_filter "tracker_id",
89 89 :type => :list, :values => trackers.collect{|s| [s.name, s.id.to_s] }
90 90
91 91 add_available_filter "priority_id",
92 92 :type => :list, :values => IssuePriority.all.collect{|s| [s.name, s.id.to_s] }
93 93
94 94 add_available_filter("author_id",
95 95 :type => :list, :values => lambda { author_values }
96 96 )
97 97
98 98 add_available_filter("assigned_to_id",
99 99 :type => :list_optional, :values => lambda { assigned_to_values }
100 100 )
101 101
102 102 add_available_filter("member_of_group",
103 103 :type => :list_optional, :values => lambda { Group.givable.visible.collect {|g| [g.name, g.id.to_s] } }
104 104 )
105 105
106 106 add_available_filter("assigned_to_role",
107 107 :type => :list_optional, :values => lambda { Role.givable.collect {|r| [r.name, r.id.to_s] } }
108 108 )
109 109
110 110 add_available_filter "fixed_version_id",
111 111 :type => :list_optional, :values => lambda { fixed_version_values }
112 112
113 113 add_available_filter "fixed_version.due_date",
114 114 :type => :date,
115 115 :name => l(:label_attribute_of_fixed_version, :name => l(:field_effective_date))
116 116
117 117 add_available_filter "fixed_version.status",
118 118 :type => :list,
119 119 :name => l(:label_attribute_of_fixed_version, :name => l(:field_status)),
120 120 :values => Version::VERSION_STATUSES.map{|s| [l("version_status_#{s}"), s] }
121 121
122 122 add_available_filter "category_id",
123 123 :type => :list_optional,
124 124 :values => lambda { project.issue_categories.collect{|s| [s.name, s.id.to_s] } } if project
125 125
126 126 add_available_filter "subject", :type => :text
127 127 add_available_filter "description", :type => :text
128 128 add_available_filter "created_on", :type => :date_past
129 129 add_available_filter "updated_on", :type => :date_past
130 130 add_available_filter "closed_on", :type => :date_past
131 131 add_available_filter "start_date", :type => :date
132 132 add_available_filter "due_date", :type => :date
133 133 add_available_filter "estimated_hours", :type => :float
134 134 add_available_filter "done_ratio", :type => :integer
135 135
136 136 if User.current.allowed_to?(:set_issues_private, nil, :global => true) ||
137 137 User.current.allowed_to?(:set_own_issues_private, nil, :global => true)
138 138 add_available_filter "is_private",
139 139 :type => :list,
140 140 :values => [[l(:general_text_yes), "1"], [l(:general_text_no), "0"]]
141 141 end
142 142
143 143 if User.current.logged?
144 144 add_available_filter "watcher_id",
145 145 :type => :list, :values => [["<< #{l(:label_me)} >>", "me"]]
146 146 end
147 147
148 add_available_filter("updated_by",
149 :type => :list, :values => lambda { author_values }
150 )
151
152 add_available_filter("last_updated_by",
153 :type => :list, :values => lambda { author_values }
154 )
155
148 156 if project && !project.leaf?
149 157 add_available_filter "subproject_id",
150 158 :type => :list_subprojects,
151 159 :values => lambda { subproject_values }
152 160 end
153 161
154 162
155 163 issue_custom_fields = project ? project.all_issue_custom_fields : IssueCustomField.where(:is_for_all => true)
156 164 add_custom_fields_filters(issue_custom_fields)
157 165
158 166 add_associations_custom_fields_filters :project, :author, :assigned_to, :fixed_version
159 167
160 168 IssueRelation::TYPES.each do |relation_type, options|
161 169 add_available_filter relation_type, :type => :relation, :label => options[:name], :values => lambda {all_projects_values}
162 170 end
163 171 add_available_filter "parent_id", :type => :tree, :label => :field_parent_issue
164 172 add_available_filter "child_id", :type => :tree, :label => :label_subtask_plural
165 173
166 174 add_available_filter "issue_id", :type => :integer, :label => :label_issue
167 175
168 add_available_filter("updated_by",
169 :type => :list, :values => lambda { author_values }
170 )
171
172 add_available_filter("last_updated_by",
173 :type => :list, :values => lambda { author_values }
174 )
175
176 176 Tracker.disabled_core_fields(trackers).each {|field|
177 177 delete_available_filter field
178 178 }
179 179 end
180 180
181 181 def available_columns
182 182 return @available_columns if @available_columns
183 183 @available_columns = self.class.available_columns.dup
184 184 @available_columns += (project ?
185 185 project.all_issue_custom_fields :
186 186 IssueCustomField
187 187 ).visible.collect {|cf| QueryCustomFieldColumn.new(cf) }
188 188
189 189 if User.current.allowed_to?(:view_time_entries, project, :global => true)
190 190 index = @available_columns.find_index {|column| column.name == :total_estimated_hours}
191 191 index = (index ? index + 1 : -1)
192 192 # insert the column after total_estimated_hours or at the end
193 193 @available_columns.insert index, QueryColumn.new(:spent_hours,
194 194 :sortable => "COALESCE((SELECT SUM(hours) FROM #{TimeEntry.table_name} WHERE #{TimeEntry.table_name}.issue_id = #{Issue.table_name}.id), 0)",
195 195 :default_order => 'desc',
196 196 :caption => :label_spent_time,
197 197 :totalable => true
198 198 )
199 199 @available_columns.insert index+1, QueryColumn.new(:total_spent_hours,
200 200 :sortable => "COALESCE((SELECT SUM(hours) FROM #{TimeEntry.table_name} JOIN #{Issue.table_name} subtasks ON subtasks.id = #{TimeEntry.table_name}.issue_id" +
201 201 " WHERE subtasks.root_id = #{Issue.table_name}.root_id AND subtasks.lft >= #{Issue.table_name}.lft AND subtasks.rgt <= #{Issue.table_name}.rgt), 0)",
202 202 :default_order => 'desc',
203 203 :caption => :label_total_spent_time
204 204 )
205 205 end
206 206
207 207 if User.current.allowed_to?(:set_issues_private, nil, :global => true) ||
208 208 User.current.allowed_to?(:set_own_issues_private, nil, :global => true)
209 209 @available_columns << QueryColumn.new(:is_private, :sortable => "#{Issue.table_name}.is_private")
210 210 end
211 211
212 212 disabled_fields = Tracker.disabled_core_fields(trackers).map {|field| field.sub(/_id$/, '')}
213 213 @available_columns.reject! {|column|
214 214 disabled_fields.include?(column.name.to_s)
215 215 }
216 216
217 217 @available_columns
218 218 end
219 219
220 220 def default_columns_names
221 221 @default_columns_names ||= begin
222 222 default_columns = Setting.issue_list_default_columns.map(&:to_sym)
223 223
224 224 project.present? ? default_columns : [:project] | default_columns
225 225 end
226 226 end
227 227
228 228 def default_totalable_names
229 229 Setting.issue_list_default_totals.map(&:to_sym)
230 230 end
231 231
232 232 def base_scope
233 233 Issue.visible.joins(:status, :project).where(statement)
234 234 end
235 235
236 236 # Returns the issue count
237 237 def issue_count
238 238 base_scope.count
239 239 rescue ::ActiveRecord::StatementInvalid => e
240 240 raise StatementInvalid.new(e.message)
241 241 end
242 242
243 243 # Returns the issue count by group or nil if query is not grouped
244 244 def issue_count_by_group
245 245 grouped_query do |scope|
246 246 scope.count
247 247 end
248 248 end
249 249
250 250 # Returns sum of all the issue's estimated_hours
251 251 def total_for_estimated_hours(scope)
252 252 map_total(scope.sum(:estimated_hours)) {|t| t.to_f.round(2)}
253 253 end
254 254
255 255 # Returns sum of all the issue's time entries hours
256 256 def total_for_spent_hours(scope)
257 257 total = if group_by_column.try(:name) == :project
258 258 # TODO: remove this when https://github.com/rails/rails/issues/21922 is fixed
259 259 # We have to do a custom join without the time_entries.project_id column
260 260 # that would trigger a ambiguous column name error
261 261 scope.joins("JOIN (SELECT issue_id, hours FROM #{TimeEntry.table_name}) AS joined_time_entries ON joined_time_entries.issue_id = #{Issue.table_name}.id").
262 262 sum("joined_time_entries.hours")
263 263 else
264 264 scope.joins(:time_entries).sum("#{TimeEntry.table_name}.hours")
265 265 end
266 266 map_total(total) {|t| t.to_f.round(2)}
267 267 end
268 268
269 269 # Returns the issues
270 270 # Valid options are :order, :offset, :limit, :include, :conditions
271 271 def issues(options={})
272 272 order_option = [group_by_sort_order, options[:order]].flatten.reject(&:blank?)
273 273
274 274 scope = Issue.visible.
275 275 joins(:status, :project).
276 276 where(statement).
277 277 includes(([:status, :project] + (options[:include] || [])).uniq).
278 278 where(options[:conditions]).
279 279 order(order_option).
280 280 joins(joins_for_order_statement(order_option.join(','))).
281 281 limit(options[:limit]).
282 282 offset(options[:offset])
283 283
284 284 scope = scope.preload([:tracker, :priority, :author, :assigned_to, :fixed_version, :category] & columns.map(&:name))
285 285 if has_custom_field_column?
286 286 scope = scope.preload(:custom_values)
287 287 end
288 288
289 289 issues = scope.to_a
290 290
291 291 if has_column?(:spent_hours)
292 292 Issue.load_visible_spent_hours(issues)
293 293 end
294 294 if has_column?(:total_spent_hours)
295 295 Issue.load_visible_total_spent_hours(issues)
296 296 end
297 297 if has_column?(:relations)
298 298 Issue.load_visible_relations(issues)
299 299 end
300 300 issues
301 301 rescue ::ActiveRecord::StatementInvalid => e
302 302 raise StatementInvalid.new(e.message)
303 303 end
304 304
305 305 # Returns the issues ids
306 306 def issue_ids(options={})
307 307 order_option = [group_by_sort_order, options[:order]].flatten.reject(&:blank?)
308 308
309 309 Issue.visible.
310 310 joins(:status, :project).
311 311 where(statement).
312 312 includes(([:status, :project] + (options[:include] || [])).uniq).
313 313 references(([:status, :project] + (options[:include] || [])).uniq).
314 314 where(options[:conditions]).
315 315 order(order_option).
316 316 joins(joins_for_order_statement(order_option.join(','))).
317 317 limit(options[:limit]).
318 318 offset(options[:offset]).
319 319 pluck(:id)
320 320 rescue ::ActiveRecord::StatementInvalid => e
321 321 raise StatementInvalid.new(e.message)
322 322 end
323 323
324 324 # Returns the journals
325 325 # Valid options are :order, :offset, :limit
326 326 def journals(options={})
327 327 Journal.visible.
328 328 joins(:issue => [:project, :status]).
329 329 where(statement).
330 330 order(options[:order]).
331 331 limit(options[:limit]).
332 332 offset(options[:offset]).
333 333 preload(:details, :user, {:issue => [:project, :author, :tracker, :status]}).
334 334 to_a
335 335 rescue ::ActiveRecord::StatementInvalid => e
336 336 raise StatementInvalid.new(e.message)
337 337 end
338 338
339 339 # Returns the versions
340 340 # Valid options are :conditions
341 341 def versions(options={})
342 342 Version.visible.
343 343 where(project_statement).
344 344 where(options[:conditions]).
345 345 includes(:project).
346 346 references(:project).
347 347 to_a
348 348 rescue ::ActiveRecord::StatementInvalid => e
349 349 raise StatementInvalid.new(e.message)
350 350 end
351 351
352 352 def sql_for_updated_by_field(field, operator, value)
353 353 neg = (operator == '!' ? 'NOT' : '')
354 354 subquery = "SELECT 1 FROM #{Journal.table_name}" +
355 355 " WHERE #{Journal.table_name}.journalized_type='Issue' AND #{Journal.table_name}.journalized_id=#{Issue.table_name}.id" +
356 356 " AND (#{sql_for_field field, '=', value, Journal.table_name, 'user_id'})" +
357 357 " AND (#{Journal.visible_notes_condition(User.current, :skip_pre_condition => true)})"
358 358
359 359 "#{neg} EXISTS (#{subquery})"
360 360 end
361 361
362 362 def sql_for_last_updated_by_field(field, operator, value)
363 363 neg = (operator == '!' ? 'NOT' : '')
364 364 subquery = "SELECT 1 FROM #{Journal.table_name} sj" +
365 365 " WHERE sj.journalized_type='Issue' AND sj.journalized_id=#{Issue.table_name}.id AND (#{sql_for_field field, '=', value, 'sj', 'user_id'})" +
366 366 " AND sj.id = (SELECT MAX(#{Journal.table_name}.id) FROM #{Journal.table_name}" +
367 367 " WHERE #{Journal.table_name}.journalized_type='Issue' AND #{Journal.table_name}.journalized_id=#{Issue.table_name}.id" +
368 368 " AND (#{Journal.visible_notes_condition(User.current, :skip_pre_condition => true)}))"
369 369
370 370 "#{neg} EXISTS (#{subquery})"
371 371 end
372 372
373 373 def sql_for_watcher_id_field(field, operator, value)
374 374 db_table = Watcher.table_name
375 375 "#{Issue.table_name}.id #{ operator == '=' ? 'IN' : 'NOT IN' } (SELECT #{db_table}.watchable_id FROM #{db_table} WHERE #{db_table}.watchable_type='Issue' AND " +
376 376 sql_for_field(field, '=', value, db_table, 'user_id') + ')'
377 377 end
378 378
379 379 def sql_for_member_of_group_field(field, operator, value)
380 380 if operator == '*' # Any group
381 381 groups = Group.givable
382 382 operator = '=' # Override the operator since we want to find by assigned_to
383 383 elsif operator == "!*"
384 384 groups = Group.givable
385 385 operator = '!' # Override the operator since we want to find by assigned_to
386 386 else
387 387 groups = Group.where(:id => value).to_a
388 388 end
389 389 groups ||= []
390 390
391 391 members_of_groups = groups.inject([]) {|user_ids, group|
392 392 user_ids + group.user_ids + [group.id]
393 393 }.uniq.compact.sort.collect(&:to_s)
394 394
395 395 '(' + sql_for_field("assigned_to_id", operator, members_of_groups, Issue.table_name, "assigned_to_id", false) + ')'
396 396 end
397 397
398 398 def sql_for_assigned_to_role_field(field, operator, value)
399 399 case operator
400 400 when "*", "!*" # Member / Not member
401 401 sw = operator == "!*" ? 'NOT' : ''
402 402 nl = operator == "!*" ? "#{Issue.table_name}.assigned_to_id IS NULL OR" : ''
403 403 "(#{nl} #{Issue.table_name}.assigned_to_id #{sw} IN (SELECT DISTINCT #{Member.table_name}.user_id FROM #{Member.table_name}" +
404 404 " WHERE #{Member.table_name}.project_id = #{Issue.table_name}.project_id))"
405 405 when "=", "!"
406 406 role_cond = value.any? ?
407 407 "#{MemberRole.table_name}.role_id IN (" + value.collect{|val| "'#{self.class.connection.quote_string(val)}'"}.join(",") + ")" :
408 408 "1=0"
409 409
410 410 sw = operator == "!" ? 'NOT' : ''
411 411 nl = operator == "!" ? "#{Issue.table_name}.assigned_to_id IS NULL OR" : ''
412 412 "(#{nl} #{Issue.table_name}.assigned_to_id #{sw} IN (SELECT DISTINCT #{Member.table_name}.user_id FROM #{Member.table_name}, #{MemberRole.table_name}" +
413 413 " WHERE #{Member.table_name}.project_id = #{Issue.table_name}.project_id AND #{Member.table_name}.id = #{MemberRole.table_name}.member_id AND #{role_cond}))"
414 414 end
415 415 end
416 416
417 417 def sql_for_fixed_version_status_field(field, operator, value)
418 418 where = sql_for_field(field, operator, value, Version.table_name, "status")
419 419 version_ids = versions(:conditions => [where]).map(&:id)
420 420
421 421 nl = operator == "!" ? "#{Issue.table_name}.fixed_version_id IS NULL OR" : ''
422 422 "(#{nl} #{sql_for_field("fixed_version_id", "=", version_ids, Issue.table_name, "fixed_version_id")})"
423 423 end
424 424
425 425 def sql_for_fixed_version_due_date_field(field, operator, value)
426 426 where = sql_for_field(field, operator, value, Version.table_name, "effective_date")
427 427 version_ids = versions(:conditions => [where]).map(&:id)
428 428
429 429 nl = operator == "!*" ? "#{Issue.table_name}.fixed_version_id IS NULL OR" : ''
430 430 "(#{nl} #{sql_for_field("fixed_version_id", "=", version_ids, Issue.table_name, "fixed_version_id")})"
431 431 end
432 432
433 433 def sql_for_is_private_field(field, operator, value)
434 434 op = (operator == "=" ? 'IN' : 'NOT IN')
435 435 va = value.map {|v| v == '0' ? self.class.connection.quoted_false : self.class.connection.quoted_true}.uniq.join(',')
436 436
437 437 "#{Issue.table_name}.is_private #{op} (#{va})"
438 438 end
439 439
440 440 def sql_for_parent_id_field(field, operator, value)
441 441 case operator
442 442 when "="
443 443 "#{Issue.table_name}.parent_id = #{value.first.to_i}"
444 444 when "~"
445 445 root_id, lft, rgt = Issue.where(:id => value.first.to_i).pluck(:root_id, :lft, :rgt).first
446 446 if root_id && lft && rgt
447 447 "#{Issue.table_name}.root_id = #{root_id} AND #{Issue.table_name}.lft > #{lft} AND #{Issue.table_name}.rgt < #{rgt}"
448 448 else
449 449 "1=0"
450 450 end
451 451 when "!*"
452 452 "#{Issue.table_name}.parent_id IS NULL"
453 453 when "*"
454 454 "#{Issue.table_name}.parent_id IS NOT NULL"
455 455 end
456 456 end
457 457
458 458 def sql_for_child_id_field(field, operator, value)
459 459 case operator
460 460 when "="
461 461 parent_id = Issue.where(:id => value.first.to_i).pluck(:parent_id).first
462 462 if parent_id
463 463 "#{Issue.table_name}.id = #{parent_id}"
464 464 else
465 465 "1=0"
466 466 end
467 467 when "~"
468 468 root_id, lft, rgt = Issue.where(:id => value.first.to_i).pluck(:root_id, :lft, :rgt).first
469 469 if root_id && lft && rgt
470 470 "#{Issue.table_name}.root_id = #{root_id} AND #{Issue.table_name}.lft < #{lft} AND #{Issue.table_name}.rgt > #{rgt}"
471 471 else
472 472 "1=0"
473 473 end
474 474 when "!*"
475 475 "#{Issue.table_name}.rgt - #{Issue.table_name}.lft = 1"
476 476 when "*"
477 477 "#{Issue.table_name}.rgt - #{Issue.table_name}.lft > 1"
478 478 end
479 479 end
480 480
481 481 def sql_for_updated_on_field(field, operator, value)
482 482 case operator
483 483 when "!*"
484 484 "#{Issue.table_name}.updated_on = #{Issue.table_name}.created_on"
485 485 when "*"
486 486 "#{Issue.table_name}.updated_on > #{Issue.table_name}.created_on"
487 487 else
488 488 sql_for_field("updated_on", operator, value, Issue.table_name, "updated_on")
489 489 end
490 490 end
491 491
492 492 def sql_for_issue_id_field(field, operator, value)
493 493 if operator == "="
494 494 # accepts a comma separated list of ids
495 495 ids = value.first.to_s.scan(/\d+/).map(&:to_i)
496 496 if ids.present?
497 497 "#{Issue.table_name}.id IN (#{ids.join(",")})"
498 498 else
499 499 "1=0"
500 500 end
501 501 else
502 502 sql_for_field("id", operator, value, Issue.table_name, "id")
503 503 end
504 504 end
505 505
506 506 def sql_for_relations(field, operator, value, options={})
507 507 relation_options = IssueRelation::TYPES[field]
508 508 return relation_options unless relation_options
509 509
510 510 relation_type = field
511 511 join_column, target_join_column = "issue_from_id", "issue_to_id"
512 512 if relation_options[:reverse] || options[:reverse]
513 513 relation_type = relation_options[:reverse] || relation_type
514 514 join_column, target_join_column = target_join_column, join_column
515 515 end
516 516
517 517 sql = case operator
518 518 when "*", "!*"
519 519 op = (operator == "*" ? 'IN' : 'NOT IN')
520 520 "#{Issue.table_name}.id #{op} (SELECT DISTINCT #{IssueRelation.table_name}.#{join_column} FROM #{IssueRelation.table_name} WHERE #{IssueRelation.table_name}.relation_type = '#{self.class.connection.quote_string(relation_type)}')"
521 521 when "=", "!"
522 522 op = (operator == "=" ? 'IN' : 'NOT IN')
523 523 "#{Issue.table_name}.id #{op} (SELECT DISTINCT #{IssueRelation.table_name}.#{join_column} FROM #{IssueRelation.table_name} WHERE #{IssueRelation.table_name}.relation_type = '#{self.class.connection.quote_string(relation_type)}' AND #{IssueRelation.table_name}.#{target_join_column} = #{value.first.to_i})"
524 524 when "=p", "=!p", "!p"
525 525 op = (operator == "!p" ? 'NOT IN' : 'IN')
526 526 comp = (operator == "=!p" ? '<>' : '=')
527 527 "#{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 = '#{self.class.connection.quote_string(relation_type)}' AND #{IssueRelation.table_name}.#{target_join_column} = relissues.id AND relissues.project_id #{comp} #{value.first.to_i})"
528 528 when "*o", "!o"
529 529 op = (operator == "!o" ? 'NOT IN' : 'IN')
530 530 "#{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 = '#{self.class.connection.quote_string(relation_type)}' AND #{IssueRelation.table_name}.#{target_join_column} = relissues.id AND relissues.status_id IN (SELECT id FROM #{IssueStatus.table_name} WHERE is_closed=#{self.class.connection.quoted_false}))"
531 531 end
532 532
533 533 if relation_options[:sym] == field && !options[:reverse]
534 534 sqls = [sql, sql_for_relations(field, operator, value, :reverse => true)]
535 535 sql = sqls.join(["!", "!*", "!p"].include?(operator) ? " AND " : " OR ")
536 536 end
537 537 "(#{sql})"
538 538 end
539 539
540 540 def find_assigned_to_id_filter_values(values)
541 541 Principal.visible.where(:id => values).map {|p| [p.name, p.id.to_s]}
542 542 end
543 543 alias :find_author_id_filter_values :find_assigned_to_id_filter_values
544 544
545 545 IssueRelation::TYPES.keys.each do |relation_type|
546 546 alias_method "sql_for_#{relation_type}_field".to_sym, :sql_for_relations
547 547 end
548 548
549 549 def joins_for_order_statement(order_options)
550 550 joins = [super]
551 551
552 552 if order_options
553 553 if order_options.include?('authors')
554 554 joins << "LEFT OUTER JOIN #{User.table_name} authors ON authors.id = #{queried_table_name}.author_id"
555 555 end
556 556 if order_options.include?('users')
557 557 joins << "LEFT OUTER JOIN #{User.table_name} ON #{User.table_name}.id = #{queried_table_name}.assigned_to_id"
558 558 end
559 559 if order_options.include?('versions')
560 560 joins << "LEFT OUTER JOIN #{Version.table_name} ON #{Version.table_name}.id = #{queried_table_name}.fixed_version_id"
561 561 end
562 562 if order_options.include?('issue_categories')
563 563 joins << "LEFT OUTER JOIN #{IssueCategory.table_name} ON #{IssueCategory.table_name}.id = #{queried_table_name}.category_id"
564 564 end
565 565 if order_options.include?('trackers')
566 566 joins << "LEFT OUTER JOIN #{Tracker.table_name} ON #{Tracker.table_name}.id = #{queried_table_name}.tracker_id"
567 567 end
568 568 if order_options.include?('enumerations')
569 569 joins << "LEFT OUTER JOIN #{IssuePriority.table_name} ON #{IssuePriority.table_name}.id = #{queried_table_name}.priority_id"
570 570 end
571 571 end
572 572
573 573 joins.any? ? joins.join(' ') : nil
574 574 end
575 575 end
General Comments 0
You need to be logged in to leave comments. Login now