@@ -1,865 +1,865 | |||
|
1 | 1 | # Redmine - project management software |
|
2 | 2 | # Copyright (C) 2006-2011 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 | if %w(list date bool int).include?(custom_field.field_format) && !custom_field.multiple? |
|
61 | 61 | self.groupable = custom_field.order_statement |
|
62 | 62 | end |
|
63 | 63 | self.groupable ||= false |
|
64 | 64 | @cf = custom_field |
|
65 | 65 | end |
|
66 | 66 | |
|
67 | 67 | def caption |
|
68 | 68 | @cf.name |
|
69 | 69 | end |
|
70 | 70 | |
|
71 | 71 | def custom_field |
|
72 | 72 | @cf |
|
73 | 73 | end |
|
74 | 74 | |
|
75 | 75 | def value(issue) |
|
76 | 76 | cv = issue.custom_values.select {|v| v.custom_field_id == @cf.id}.collect {|v| @cf.cast_value(v.value)} |
|
77 | 77 | cv.size > 1 ? cv : cv.first |
|
78 | 78 | end |
|
79 | 79 | |
|
80 | 80 | def css_classes |
|
81 | 81 | @css_classes ||= "#{name} #{@cf.field_format}" |
|
82 | 82 | end |
|
83 | 83 | end |
|
84 | 84 | |
|
85 | 85 | class Query < ActiveRecord::Base |
|
86 | 86 | class StatementInvalid < ::ActiveRecord::StatementInvalid |
|
87 | 87 | end |
|
88 | 88 | |
|
89 | 89 | belongs_to :project |
|
90 | 90 | belongs_to :user |
|
91 | 91 | serialize :filters |
|
92 | 92 | serialize :column_names |
|
93 | 93 | serialize :sort_criteria, Array |
|
94 | 94 | |
|
95 | 95 | attr_protected :project_id, :user_id |
|
96 | 96 | |
|
97 | 97 | validates_presence_of :name |
|
98 | 98 | validates_length_of :name, :maximum => 255 |
|
99 | 99 | validate :validate_query_filters |
|
100 | 100 | |
|
101 | 101 | @@operators = { "=" => :label_equals, |
|
102 | 102 | "!" => :label_not_equals, |
|
103 | 103 | "o" => :label_open_issues, |
|
104 | 104 | "c" => :label_closed_issues, |
|
105 | 105 | "!*" => :label_none, |
|
106 | 106 | "*" => :label_all, |
|
107 | 107 | ">=" => :label_greater_or_equal, |
|
108 | 108 | "<=" => :label_less_or_equal, |
|
109 | 109 | "><" => :label_between, |
|
110 | 110 | "<t+" => :label_in_less_than, |
|
111 | 111 | ">t+" => :label_in_more_than, |
|
112 | 112 | "t+" => :label_in, |
|
113 | 113 | "t" => :label_today, |
|
114 | 114 | "w" => :label_this_week, |
|
115 | 115 | ">t-" => :label_less_than_ago, |
|
116 | 116 | "<t-" => :label_more_than_ago, |
|
117 | 117 | "t-" => :label_ago, |
|
118 | 118 | "~" => :label_contains, |
|
119 | 119 | "!~" => :label_not_contains } |
|
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 | |
|
134 | 134 | cattr_reader :operators_by_filter_type |
|
135 | 135 | |
|
136 | 136 | @@available_columns = [ |
|
137 | 137 | QueryColumn.new(:project, :sortable => "#{Project.table_name}.name", :groupable => true), |
|
138 | 138 | QueryColumn.new(:tracker, :sortable => "#{Tracker.table_name}.position", :groupable => true), |
|
139 | 139 | QueryColumn.new(:parent, :sortable => ["#{Issue.table_name}.root_id", "#{Issue.table_name}.lft ASC"], :default_order => 'desc', :caption => :field_parent_issue), |
|
140 | 140 | QueryColumn.new(:status, :sortable => "#{IssueStatus.table_name}.position", :groupable => true), |
|
141 | 141 | QueryColumn.new(:priority, :sortable => "#{IssuePriority.table_name}.position", :default_order => 'desc', :groupable => true), |
|
142 | 142 | QueryColumn.new(:subject, :sortable => "#{Issue.table_name}.subject"), |
|
143 | 143 | QueryColumn.new(:author, :sortable => lambda {User.fields_for_order_statement("authors")}, :groupable => true), |
|
144 | 144 | QueryColumn.new(:assigned_to, :sortable => lambda {User.fields_for_order_statement}, :groupable => true), |
|
145 | 145 | QueryColumn.new(:updated_on, :sortable => "#{Issue.table_name}.updated_on", :default_order => 'desc'), |
|
146 | 146 | QueryColumn.new(:category, :sortable => "#{IssueCategory.table_name}.name", :groupable => true), |
|
147 | 147 | QueryColumn.new(:fixed_version, :sortable => ["#{Version.table_name}.effective_date", "#{Version.table_name}.name"], :default_order => 'desc', :groupable => true), |
|
148 | 148 | QueryColumn.new(:start_date, :sortable => "#{Issue.table_name}.start_date"), |
|
149 | 149 | QueryColumn.new(:due_date, :sortable => "#{Issue.table_name}.due_date"), |
|
150 | 150 | QueryColumn.new(:estimated_hours, :sortable => "#{Issue.table_name}.estimated_hours"), |
|
151 | 151 | QueryColumn.new(:done_ratio, :sortable => "#{Issue.table_name}.done_ratio", :groupable => true), |
|
152 | 152 | QueryColumn.new(:created_on, :sortable => "#{Issue.table_name}.created_on", :default_order => 'desc'), |
|
153 | 153 | ] |
|
154 | 154 | cattr_reader :available_columns |
|
155 | 155 | |
|
156 | 156 | named_scope :visible, lambda {|*args| |
|
157 | 157 | user = args.shift || User.current |
|
158 | 158 | base = Project.allowed_to_condition(user, :view_issues, *args) |
|
159 | 159 | user_id = user.logged? ? user.id : 0 |
|
160 | 160 | { |
|
161 | 161 | :conditions => ["(#{table_name}.project_id IS NULL OR (#{base})) AND (#{table_name}.is_public = ? OR #{table_name}.user_id = ?)", true, user_id], |
|
162 | 162 | :include => :project |
|
163 | 163 | } |
|
164 | 164 | } |
|
165 | 165 | |
|
166 | 166 | def initialize(attributes=nil, *args) |
|
167 | 167 | super attributes |
|
168 | 168 | self.filters ||= { 'status_id' => {:operator => "o", :values => [""]} } |
|
169 | 169 | @is_for_all = project.nil? |
|
170 | 170 | end |
|
171 | 171 | |
|
172 | 172 | def validate_query_filters |
|
173 | 173 | filters.each_key do |field| |
|
174 | 174 | if values_for(field) |
|
175 | 175 | case type_for(field) |
|
176 | 176 | when :integer |
|
177 | 177 | add_filter_error(field, :invalid) if values_for(field).detect {|v| v.present? && !v.match(/^\d+$/) } |
|
178 | 178 | when :float |
|
179 | 179 | add_filter_error(field, :invalid) if values_for(field).detect {|v| v.present? && !v.match(/^\d+(\.\d*)?$/) } |
|
180 | 180 | when :date, :date_past |
|
181 | 181 | case operator_for(field) |
|
182 | 182 | when "=", ">=", "<=", "><" |
|
183 | 183 | 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?) } |
|
184 | 184 | when ">t-", "<t-", "t-" |
|
185 | 185 | add_filter_error(field, :invalid) if values_for(field).detect {|v| v.present? && !v.match(/^\d+$/) } |
|
186 | 186 | end |
|
187 | 187 | end |
|
188 | 188 | end |
|
189 | 189 | |
|
190 | 190 | add_filter_error(field, :blank) unless |
|
191 | 191 | # filter requires one or more values |
|
192 | 192 | (values_for(field) and !values_for(field).first.blank?) or |
|
193 | 193 | # filter doesn't require any value |
|
194 | 194 | ["o", "c", "!*", "*", "t", "w"].include? operator_for(field) |
|
195 | 195 | end if filters |
|
196 | 196 | end |
|
197 | 197 | |
|
198 | 198 | def add_filter_error(field, message) |
|
199 | 199 | m = label_for(field) + " " + l(message, :scope => 'activerecord.errors.messages') |
|
200 | 200 | errors.add(:base, m) |
|
201 | 201 | end |
|
202 | 202 | |
|
203 | 203 | # Returns true if the query is visible to +user+ or the current user. |
|
204 | 204 | def visible?(user=User.current) |
|
205 | 205 | (project.nil? || user.allowed_to?(:view_issues, project)) && (self.is_public? || self.user_id == user.id) |
|
206 | 206 | end |
|
207 | 207 | |
|
208 | 208 | def editable_by?(user) |
|
209 | 209 | return false unless user |
|
210 | 210 | # Admin can edit them all and regular users can edit their private queries |
|
211 | 211 | return true if user.admin? || (!is_public && self.user_id == user.id) |
|
212 | 212 | # Members can not edit public queries that are for all project (only admin is allowed to) |
|
213 | 213 | is_public && !@is_for_all && user.allowed_to?(:manage_public_queries, project) |
|
214 | 214 | end |
|
215 | 215 | |
|
216 | 216 | def available_filters |
|
217 | 217 | return @available_filters if @available_filters |
|
218 | 218 | |
|
219 | 219 | trackers = project.nil? ? Tracker.find(:all, :order => 'position') : project.rolled_up_trackers |
|
220 | 220 | |
|
221 | 221 | @available_filters = { "status_id" => { :type => :list_status, :order => 1, :values => IssueStatus.find(:all, :order => 'position').collect{|s| [s.name, s.id.to_s] } }, |
|
222 | 222 | "tracker_id" => { :type => :list, :order => 2, :values => trackers.collect{|s| [s.name, s.id.to_s] } }, |
|
223 | 223 | "priority_id" => { :type => :list, :order => 3, :values => IssuePriority.all.collect{|s| [s.name, s.id.to_s] } }, |
|
224 | 224 | "subject" => { :type => :text, :order => 8 }, |
|
225 | 225 | "created_on" => { :type => :date_past, :order => 9 }, |
|
226 | 226 | "updated_on" => { :type => :date_past, :order => 10 }, |
|
227 | 227 | "start_date" => { :type => :date, :order => 11 }, |
|
228 | 228 | "due_date" => { :type => :date, :order => 12 }, |
|
229 | 229 | "estimated_hours" => { :type => :float, :order => 13 }, |
|
230 | 230 | "done_ratio" => { :type => :integer, :order => 14 }} |
|
231 | 231 | |
|
232 | 232 | principals = [] |
|
233 | 233 | if project |
|
234 | 234 | principals += project.principals.sort |
|
235 | 235 | unless project.leaf? |
|
236 | 236 | subprojects = project.descendants.visible.all |
|
237 | 237 | if subprojects.any? |
|
238 | 238 | @available_filters["subproject_id"] = { :type => :list_subprojects, :order => 13, :values => subprojects.collect{|s| [s.name, s.id.to_s] } } |
|
239 | 239 | principals += Principal.member_of(subprojects) |
|
240 | 240 | end |
|
241 | 241 | end |
|
242 | 242 | else |
|
243 | 243 | all_projects = Project.visible.all |
|
244 | 244 | if all_projects.any? |
|
245 | 245 | # members of visible projects |
|
246 | 246 | principals += Principal.member_of(all_projects) |
|
247 | 247 | |
|
248 | 248 | # project filter |
|
249 | 249 | project_values = [] |
|
250 | 250 | if User.current.logged? && User.current.memberships.any? |
|
251 | 251 | project_values << ["<< #{l(:label_my_projects).downcase} >>", "mine"] |
|
252 | 252 | end |
|
253 | 253 | Project.project_tree(all_projects) do |p, level| |
|
254 | 254 | prefix = (level > 0 ? ('--' * level + ' ') : '') |
|
255 | 255 | project_values << ["#{prefix}#{p.name}", p.id.to_s] |
|
256 | 256 | end |
|
257 | 257 | @available_filters["project_id"] = { :type => :list, :order => 1, :values => project_values} unless project_values.empty? |
|
258 | 258 | end |
|
259 | 259 | end |
|
260 | 260 | principals.uniq! |
|
261 | 261 | principals.sort! |
|
262 | 262 | users = principals.select {|p| p.is_a?(User)} |
|
263 | 263 | |
|
264 | 264 | assigned_to_values = [] |
|
265 | 265 | assigned_to_values << ["<< #{l(:label_me)} >>", "me"] if User.current.logged? |
|
266 | 266 | assigned_to_values += (Setting.issue_group_assignment? ? principals : users).collect{|s| [s.name, s.id.to_s] } |
|
267 | 267 | @available_filters["assigned_to_id"] = { :type => :list_optional, :order => 4, :values => assigned_to_values } unless assigned_to_values.empty? |
|
268 | 268 | |
|
269 | 269 | author_values = [] |
|
270 | 270 | author_values << ["<< #{l(:label_me)} >>", "me"] if User.current.logged? |
|
271 | 271 | author_values += users.collect{|s| [s.name, s.id.to_s] } |
|
272 | 272 | @available_filters["author_id"] = { :type => :list, :order => 5, :values => author_values } unless author_values.empty? |
|
273 | 273 | |
|
274 | 274 | group_values = Group.all.collect {|g| [g.name, g.id.to_s] } |
|
275 | 275 | @available_filters["member_of_group"] = { :type => :list_optional, :order => 6, :values => group_values } unless group_values.empty? |
|
276 | 276 | |
|
277 | 277 | role_values = Role.givable.collect {|r| [r.name, r.id.to_s] } |
|
278 | 278 | @available_filters["assigned_to_role"] = { :type => :list_optional, :order => 7, :values => role_values } unless role_values.empty? |
|
279 | 279 | |
|
280 | 280 | if User.current.logged? |
|
281 | 281 | @available_filters["watcher_id"] = { :type => :list, :order => 15, :values => [["<< #{l(:label_me)} >>", "me"]] } |
|
282 | 282 | end |
|
283 | 283 | |
|
284 | 284 | if project |
|
285 | 285 | # project specific filters |
|
286 | 286 | categories = project.issue_categories.all |
|
287 | 287 | unless categories.empty? |
|
288 | 288 | @available_filters["category_id"] = { :type => :list_optional, :order => 6, :values => categories.collect{|s| [s.name, s.id.to_s] } } |
|
289 | 289 | end |
|
290 | 290 | versions = project.shared_versions.all |
|
291 | 291 | unless versions.empty? |
|
292 | 292 | @available_filters["fixed_version_id"] = { :type => :list_optional, :order => 7, :values => versions.sort.collect{|s| ["#{s.project.name} - #{s.name}", s.id.to_s] } } |
|
293 | 293 | end |
|
294 | 294 | add_custom_fields_filters(project.all_issue_custom_fields) |
|
295 | 295 | else |
|
296 | 296 | # global filters for cross project issue list |
|
297 | 297 | system_shared_versions = Version.visible.find_all_by_sharing('system') |
|
298 | 298 | unless system_shared_versions.empty? |
|
299 | 299 | @available_filters["fixed_version_id"] = { :type => :list_optional, :order => 7, :values => system_shared_versions.sort.collect{|s| ["#{s.project.name} - #{s.name}", s.id.to_s] } } |
|
300 | 300 | end |
|
301 | 301 | add_custom_fields_filters(IssueCustomField.find(:all, :conditions => {:is_filter => true, :is_for_all => true})) |
|
302 | 302 | end |
|
303 | 303 | @available_filters |
|
304 | 304 | end |
|
305 | 305 | |
|
306 | 306 | def add_filter(field, operator, values) |
|
307 | 307 | # values must be an array |
|
308 | 308 | return unless values.nil? || values.is_a?(Array) |
|
309 | 309 | # check if field is defined as an available filter |
|
310 | 310 | if available_filters.has_key? field |
|
311 | 311 | filter_options = available_filters[field] |
|
312 | 312 | # check if operator is allowed for that filter |
|
313 | 313 | #if @@operators_by_filter_type[filter_options[:type]].include? operator |
|
314 | 314 | # allowed_values = values & ([""] + (filter_options[:values] || []).collect {|val| val[1]}) |
|
315 | 315 | # filters[field] = {:operator => operator, :values => allowed_values } if (allowed_values.first and !allowed_values.first.empty?) or ["o", "c", "!*", "*", "t"].include? operator |
|
316 | 316 | #end |
|
317 | 317 | filters[field] = {:operator => operator, :values => (values || [''])} |
|
318 | 318 | end |
|
319 | 319 | end |
|
320 | 320 | |
|
321 | 321 | def add_short_filter(field, expression) |
|
322 | 322 | return unless expression && available_filters.has_key?(field) |
|
323 | 323 | field_type = available_filters[field][:type] |
|
324 | 324 | @@operators_by_filter_type[field_type].sort.reverse.detect do |operator| |
|
325 | 325 | next unless expression =~ /^#{Regexp.escape(operator)}(.*)$/ |
|
326 | 326 | add_filter field, operator, $1.present? ? $1.split('|') : [''] |
|
327 | 327 | end || add_filter(field, '=', expression.split('|')) |
|
328 | 328 | end |
|
329 | 329 | |
|
330 | 330 | # Add multiple filters using +add_filter+ |
|
331 | 331 | def add_filters(fields, operators, values) |
|
332 | 332 | if fields.is_a?(Array) && operators.is_a?(Hash) && (values.nil? || values.is_a?(Hash)) |
|
333 | 333 | fields.each do |field| |
|
334 | 334 | add_filter(field, operators[field], values && values[field]) |
|
335 | 335 | end |
|
336 | 336 | end |
|
337 | 337 | end |
|
338 | 338 | |
|
339 | 339 | def has_filter?(field) |
|
340 | 340 | filters and filters[field] |
|
341 | 341 | end |
|
342 | 342 | |
|
343 | 343 | def type_for(field) |
|
344 | 344 | available_filters[field][:type] if available_filters.has_key?(field) |
|
345 | 345 | end |
|
346 | 346 | |
|
347 | 347 | def operator_for(field) |
|
348 | 348 | has_filter?(field) ? filters[field][:operator] : nil |
|
349 | 349 | end |
|
350 | 350 | |
|
351 | 351 | def values_for(field) |
|
352 | 352 | has_filter?(field) ? filters[field][:values] : nil |
|
353 | 353 | end |
|
354 | 354 | |
|
355 | 355 | def value_for(field, index=0) |
|
356 | 356 | (values_for(field) || [])[index] |
|
357 | 357 | end |
|
358 | 358 | |
|
359 | 359 | def label_for(field) |
|
360 | 360 | label = available_filters[field][:name] if available_filters.has_key?(field) |
|
361 | 361 | label ||= l("field_#{field.to_s.gsub(/_id$/, '')}", :default => field) |
|
362 | 362 | end |
|
363 | 363 | |
|
364 | 364 | def available_columns |
|
365 | 365 | return @available_columns if @available_columns |
|
366 | 366 | @available_columns = ::Query.available_columns.dup |
|
367 | 367 | @available_columns += (project ? |
|
368 | 368 | project.all_issue_custom_fields : |
|
369 | 369 | IssueCustomField.find(:all) |
|
370 | 370 | ).collect {|cf| QueryCustomFieldColumn.new(cf) } |
|
371 | 371 | |
|
372 | 372 | if User.current.allowed_to?(:view_time_entries, project, :global => true) |
|
373 | 373 | index = nil |
|
374 | 374 | @available_columns.each_with_index {|column, i| index = i if column.name == :estimated_hours} |
|
375 | 375 | index = (index ? index + 1 : -1) |
|
376 | 376 | # insert the column after estimated_hours or at the end |
|
377 | 377 | @available_columns.insert index, QueryColumn.new(:spent_hours, |
|
378 | 378 | :sortable => "(SELECT COALESCE(SUM(hours), 0) FROM #{TimeEntry.table_name} WHERE #{TimeEntry.table_name}.issue_id = #{Issue.table_name}.id)", |
|
379 | 379 | :default_order => 'desc', |
|
380 | 380 | :caption => :label_spent_time |
|
381 | 381 | ) |
|
382 | 382 | end |
|
383 | 383 | @available_columns |
|
384 | 384 | end |
|
385 | 385 | |
|
386 | 386 | def self.available_columns=(v) |
|
387 | 387 | self.available_columns = (v) |
|
388 | 388 | end |
|
389 | 389 | |
|
390 | 390 | def self.add_available_column(column) |
|
391 | 391 | self.available_columns << (column) if column.is_a?(QueryColumn) |
|
392 | 392 | end |
|
393 | 393 | |
|
394 | 394 | # Returns an array of columns that can be used to group the results |
|
395 | 395 | def groupable_columns |
|
396 | 396 | available_columns.select {|c| c.groupable} |
|
397 | 397 | end |
|
398 | 398 | |
|
399 | 399 | # Returns a Hash of columns and the key for sorting |
|
400 | 400 | def sortable_columns |
|
401 | 401 | {'id' => "#{Issue.table_name}.id"}.merge(available_columns.inject({}) {|h, column| |
|
402 | 402 | h[column.name.to_s] = column.sortable |
|
403 | 403 | h |
|
404 | 404 | }) |
|
405 | 405 | end |
|
406 | 406 | |
|
407 | 407 | def columns |
|
408 | 408 | # preserve the column_names order |
|
409 | 409 | (has_default_columns? ? default_columns_names : column_names).collect do |name| |
|
410 | 410 | available_columns.find { |col| col.name == name } |
|
411 | 411 | end.compact |
|
412 | 412 | end |
|
413 | 413 | |
|
414 | 414 | def default_columns_names |
|
415 | 415 | @default_columns_names ||= begin |
|
416 | 416 | default_columns = Setting.issue_list_default_columns.map(&:to_sym) |
|
417 | 417 | |
|
418 | 418 | project.present? ? default_columns : [:project] | default_columns |
|
419 | 419 | end |
|
420 | 420 | end |
|
421 | 421 | |
|
422 | 422 | def column_names=(names) |
|
423 | 423 | if names |
|
424 | 424 | names = names.select {|n| n.is_a?(Symbol) || !n.blank? } |
|
425 | 425 | names = names.collect {|n| n.is_a?(Symbol) ? n : n.to_sym } |
|
426 | 426 | # Set column_names to nil if default columns |
|
427 | 427 | if names == default_columns_names |
|
428 | 428 | names = nil |
|
429 | 429 | end |
|
430 | 430 | end |
|
431 | 431 | write_attribute(:column_names, names) |
|
432 | 432 | end |
|
433 | 433 | |
|
434 | 434 | def has_column?(column) |
|
435 | 435 | column_names && column_names.include?(column.is_a?(QueryColumn) ? column.name : column) |
|
436 | 436 | end |
|
437 | 437 | |
|
438 | 438 | def has_default_columns? |
|
439 | 439 | column_names.nil? || column_names.empty? |
|
440 | 440 | end |
|
441 | 441 | |
|
442 | 442 | def sort_criteria=(arg) |
|
443 | 443 | c = [] |
|
444 | 444 | if arg.is_a?(Hash) |
|
445 | 445 | arg = arg.keys.sort.collect {|k| arg[k]} |
|
446 | 446 | end |
|
447 | 447 | c = arg.select {|k,o| !k.to_s.blank?}.slice(0,3).collect {|k,o| [k.to_s, o == 'desc' ? o : 'asc']} |
|
448 | 448 | write_attribute(:sort_criteria, c) |
|
449 | 449 | end |
|
450 | 450 | |
|
451 | 451 | def sort_criteria |
|
452 | 452 | read_attribute(:sort_criteria) || [] |
|
453 | 453 | end |
|
454 | 454 | |
|
455 | 455 | def sort_criteria_key(arg) |
|
456 | 456 | sort_criteria && sort_criteria[arg] && sort_criteria[arg].first |
|
457 | 457 | end |
|
458 | 458 | |
|
459 | 459 | def sort_criteria_order(arg) |
|
460 | 460 | sort_criteria && sort_criteria[arg] && sort_criteria[arg].last |
|
461 | 461 | end |
|
462 | 462 | |
|
463 | 463 | # Returns the SQL sort order that should be prepended for grouping |
|
464 | 464 | def group_by_sort_order |
|
465 | 465 | if grouped? && (column = group_by_column) |
|
466 | 466 | column.sortable.is_a?(Array) ? |
|
467 | 467 | column.sortable.collect {|s| "#{s} #{column.default_order}"}.join(',') : |
|
468 | 468 | "#{column.sortable} #{column.default_order}" |
|
469 | 469 | end |
|
470 | 470 | end |
|
471 | 471 | |
|
472 | 472 | # Returns true if the query is a grouped query |
|
473 | 473 | def grouped? |
|
474 | 474 | !group_by_column.nil? |
|
475 | 475 | end |
|
476 | 476 | |
|
477 | 477 | def group_by_column |
|
478 | 478 | groupable_columns.detect {|c| c.groupable && c.name.to_s == group_by} |
|
479 | 479 | end |
|
480 | 480 | |
|
481 | 481 | def group_by_statement |
|
482 | 482 | group_by_column.try(:groupable) |
|
483 | 483 | end |
|
484 | 484 | |
|
485 | 485 | def project_statement |
|
486 | 486 | project_clauses = [] |
|
487 | 487 | if project && !project.descendants.active.empty? |
|
488 | 488 | ids = [project.id] |
|
489 | 489 | if has_filter?("subproject_id") |
|
490 | 490 | case operator_for("subproject_id") |
|
491 | 491 | when '=' |
|
492 | 492 | # include the selected subprojects |
|
493 | 493 | ids += values_for("subproject_id").each(&:to_i) |
|
494 | 494 | when '!*' |
|
495 | 495 | # main project only |
|
496 | 496 | else |
|
497 | 497 | # all subprojects |
|
498 | 498 | ids += project.descendants.collect(&:id) |
|
499 | 499 | end |
|
500 | 500 | elsif Setting.display_subprojects_issues? |
|
501 | 501 | ids += project.descendants.collect(&:id) |
|
502 | 502 | end |
|
503 | 503 | project_clauses << "#{Project.table_name}.id IN (%s)" % ids.join(',') |
|
504 | 504 | elsif project |
|
505 | 505 | project_clauses << "#{Project.table_name}.id = %d" % project.id |
|
506 | 506 | end |
|
507 | 507 | project_clauses.any? ? project_clauses.join(' AND ') : nil |
|
508 | 508 | end |
|
509 | 509 | |
|
510 | 510 | def statement |
|
511 | 511 | # filters clauses |
|
512 | 512 | filters_clauses = [] |
|
513 | 513 | filters.each_key do |field| |
|
514 | 514 | next if field == "subproject_id" |
|
515 | 515 | v = values_for(field).clone |
|
516 | 516 | next unless v and !v.empty? |
|
517 | 517 | operator = operator_for(field) |
|
518 | 518 | |
|
519 | 519 | # "me" value subsitution |
|
520 | 520 | if %w(assigned_to_id author_id watcher_id).include?(field) |
|
521 | 521 | if v.delete("me") |
|
522 | 522 | if User.current.logged? |
|
523 | 523 | v.push(User.current.id.to_s) |
|
524 | 524 | v += User.current.group_ids.map(&:to_s) if field == 'assigned_to_id' |
|
525 | 525 | else |
|
526 | 526 | v.push("0") |
|
527 | 527 | end |
|
528 | 528 | end |
|
529 | 529 | end |
|
530 | 530 | |
|
531 | 531 | if field == 'project_id' |
|
532 | 532 | if v.delete('mine') |
|
533 | 533 | v += User.current.memberships.map(&:project_id).map(&:to_s) |
|
534 | 534 | end |
|
535 | 535 | end |
|
536 | 536 | |
|
537 | 537 | if field =~ /^cf_(\d+)$/ |
|
538 | 538 | # custom field |
|
539 | 539 | filters_clauses << sql_for_custom_field(field, operator, v, $1) |
|
540 | 540 | elsif respond_to?("sql_for_#{field}_field") |
|
541 | 541 | # specific statement |
|
542 | 542 | filters_clauses << send("sql_for_#{field}_field", field, operator, v) |
|
543 | 543 | else |
|
544 | 544 | # regular field |
|
545 | 545 | filters_clauses << '(' + sql_for_field(field, operator, v, Issue.table_name, field) + ')' |
|
546 | 546 | end |
|
547 | 547 | end if filters and valid? |
|
548 | 548 | |
|
549 | 549 | filters_clauses << project_statement |
|
550 | 550 | filters_clauses.reject!(&:blank?) |
|
551 | 551 | |
|
552 | 552 | filters_clauses.any? ? filters_clauses.join(' AND ') : nil |
|
553 | 553 | end |
|
554 | 554 | |
|
555 | 555 | # Returns the issue count |
|
556 | 556 | def issue_count |
|
557 | 557 | Issue.visible.count(:include => [:status, :project], :conditions => statement) |
|
558 | 558 | rescue ::ActiveRecord::StatementInvalid => e |
|
559 | 559 | raise StatementInvalid.new(e.message) |
|
560 | 560 | end |
|
561 | 561 | |
|
562 | 562 | # Returns the issue count by group or nil if query is not grouped |
|
563 | 563 | def issue_count_by_group |
|
564 | 564 | r = nil |
|
565 | 565 | if grouped? |
|
566 | 566 | begin |
|
567 | 567 | # Rails will raise an (unexpected) RecordNotFound if there's only a nil group value |
|
568 | 568 | r = Issue.visible.count(:group => group_by_statement, :include => [:status, :project], :conditions => statement) |
|
569 | 569 | rescue ActiveRecord::RecordNotFound |
|
570 | 570 | r = {nil => issue_count} |
|
571 | 571 | end |
|
572 | 572 | c = group_by_column |
|
573 | 573 | if c.is_a?(QueryCustomFieldColumn) |
|
574 | 574 | r = r.keys.inject({}) {|h, k| h[c.custom_field.cast_value(k)] = r[k]; h} |
|
575 | 575 | end |
|
576 | 576 | end |
|
577 | 577 | r |
|
578 | 578 | rescue ::ActiveRecord::StatementInvalid => e |
|
579 | 579 | raise StatementInvalid.new(e.message) |
|
580 | 580 | end |
|
581 | 581 | |
|
582 | 582 | # Returns the issues |
|
583 | 583 | # Valid options are :order, :offset, :limit, :include, :conditions |
|
584 | 584 | def issues(options={}) |
|
585 | 585 | order_option = [group_by_sort_order, options[:order]].reject {|s| s.blank?}.join(',') |
|
586 | 586 | order_option = nil if order_option.blank? |
|
587 | 587 | |
|
588 | 588 | joins = (order_option && order_option.include?('authors')) ? "LEFT OUTER JOIN users authors ON authors.id = #{Issue.table_name}.author_id" : nil |
|
589 | 589 | |
|
590 | 590 | issues = Issue.visible.scoped(:conditions => options[:conditions]).find :all, :include => ([:status, :project] + (options[:include] || [])).uniq, |
|
591 | 591 | :conditions => statement, |
|
592 | 592 | :order => order_option, |
|
593 | 593 | :joins => joins, |
|
594 | 594 | :limit => options[:limit], |
|
595 | 595 | :offset => options[:offset] |
|
596 | 596 | |
|
597 | 597 | if has_column?(:spent_hours) |
|
598 | 598 | Issue.load_visible_spent_hours(issues) |
|
599 | 599 | end |
|
600 | 600 | issues |
|
601 | 601 | rescue ::ActiveRecord::StatementInvalid => e |
|
602 | 602 | raise StatementInvalid.new(e.message) |
|
603 | 603 | end |
|
604 | 604 | |
|
605 | 605 | # Returns the issues ids |
|
606 | 606 | def issue_ids(options={}) |
|
607 | 607 | order_option = [group_by_sort_order, options[:order]].reject {|s| s.blank?}.join(',') |
|
608 | 608 | order_option = nil if order_option.blank? |
|
609 | 609 | |
|
610 | 610 | joins = (order_option && order_option.include?('authors')) ? "LEFT OUTER JOIN users authors ON authors.id = #{Issue.table_name}.author_id" : nil |
|
611 | 611 | |
|
612 |
Issue.visible.scoped(:conditions => options[:conditions]). |
|
|
612 | Issue.visible.scoped(:conditions => options[:conditions]).scoped(:include => ([:status, :project] + (options[:include] || [])).uniq, | |
|
613 | 613 | :conditions => statement, |
|
614 | 614 | :order => order_option, |
|
615 | 615 | :joins => joins, |
|
616 | 616 | :limit => options[:limit], |
|
617 | :offset => options[:offset] | |
|
617 | :offset => options[:offset]).find_ids | |
|
618 | 618 | rescue ::ActiveRecord::StatementInvalid => e |
|
619 | 619 | raise StatementInvalid.new(e.message) |
|
620 | 620 | end |
|
621 | 621 | |
|
622 | 622 | # Returns the journals |
|
623 | 623 | # Valid options are :order, :offset, :limit |
|
624 | 624 | def journals(options={}) |
|
625 | 625 | Journal.visible.find :all, :include => [:details, :user, {:issue => [:project, :author, :tracker, :status]}], |
|
626 | 626 | :conditions => statement, |
|
627 | 627 | :order => options[:order], |
|
628 | 628 | :limit => options[:limit], |
|
629 | 629 | :offset => options[:offset] |
|
630 | 630 | rescue ::ActiveRecord::StatementInvalid => e |
|
631 | 631 | raise StatementInvalid.new(e.message) |
|
632 | 632 | end |
|
633 | 633 | |
|
634 | 634 | # Returns the versions |
|
635 | 635 | # Valid options are :conditions |
|
636 | 636 | def versions(options={}) |
|
637 | 637 | Version.visible.scoped(:conditions => options[:conditions]).find :all, :include => :project, :conditions => project_statement |
|
638 | 638 | rescue ::ActiveRecord::StatementInvalid => e |
|
639 | 639 | raise StatementInvalid.new(e.message) |
|
640 | 640 | end |
|
641 | 641 | |
|
642 | 642 | def sql_for_watcher_id_field(field, operator, value) |
|
643 | 643 | db_table = Watcher.table_name |
|
644 | 644 | "#{Issue.table_name}.id #{ operator == '=' ? 'IN' : 'NOT IN' } (SELECT #{db_table}.watchable_id FROM #{db_table} WHERE #{db_table}.watchable_type='Issue' AND " + |
|
645 | 645 | sql_for_field(field, '=', value, db_table, 'user_id') + ')' |
|
646 | 646 | end |
|
647 | 647 | |
|
648 | 648 | def sql_for_member_of_group_field(field, operator, value) |
|
649 | 649 | if operator == '*' # Any group |
|
650 | 650 | groups = Group.all |
|
651 | 651 | operator = '=' # Override the operator since we want to find by assigned_to |
|
652 | 652 | elsif operator == "!*" |
|
653 | 653 | groups = Group.all |
|
654 | 654 | operator = '!' # Override the operator since we want to find by assigned_to |
|
655 | 655 | else |
|
656 | 656 | groups = Group.find_all_by_id(value) |
|
657 | 657 | end |
|
658 | 658 | groups ||= [] |
|
659 | 659 | |
|
660 | 660 | members_of_groups = groups.inject([]) {|user_ids, group| |
|
661 | 661 | if group && group.user_ids.present? |
|
662 | 662 | user_ids << group.user_ids |
|
663 | 663 | end |
|
664 | 664 | user_ids.flatten.uniq.compact |
|
665 | 665 | }.sort.collect(&:to_s) |
|
666 | 666 | |
|
667 | 667 | '(' + sql_for_field("assigned_to_id", operator, members_of_groups, Issue.table_name, "assigned_to_id", false) + ')' |
|
668 | 668 | end |
|
669 | 669 | |
|
670 | 670 | def sql_for_assigned_to_role_field(field, operator, value) |
|
671 | 671 | case operator |
|
672 | 672 | when "*", "!*" # Member / Not member |
|
673 | 673 | sw = operator == "!*" ? 'NOT' : '' |
|
674 | 674 | nl = operator == "!*" ? "#{Issue.table_name}.assigned_to_id IS NULL OR" : '' |
|
675 | 675 | "(#{nl} #{Issue.table_name}.assigned_to_id #{sw} IN (SELECT DISTINCT #{Member.table_name}.user_id FROM #{Member.table_name}" + |
|
676 | 676 | " WHERE #{Member.table_name}.project_id = #{Issue.table_name}.project_id))" |
|
677 | 677 | when "=", "!" |
|
678 | 678 | role_cond = value.any? ? |
|
679 | 679 | "#{MemberRole.table_name}.role_id IN (" + value.collect{|val| "'#{connection.quote_string(val)}'"}.join(",") + ")" : |
|
680 | 680 | "1=0" |
|
681 | 681 | |
|
682 | 682 | sw = operator == "!" ? 'NOT' : '' |
|
683 | 683 | nl = operator == "!" ? "#{Issue.table_name}.assigned_to_id IS NULL OR" : '' |
|
684 | 684 | "(#{nl} #{Issue.table_name}.assigned_to_id #{sw} IN (SELECT DISTINCT #{Member.table_name}.user_id FROM #{Member.table_name}, #{MemberRole.table_name}" + |
|
685 | 685 | " WHERE #{Member.table_name}.project_id = #{Issue.table_name}.project_id AND #{Member.table_name}.id = #{MemberRole.table_name}.member_id AND #{role_cond}))" |
|
686 | 686 | end |
|
687 | 687 | end |
|
688 | 688 | |
|
689 | 689 | private |
|
690 | 690 | |
|
691 | 691 | def sql_for_custom_field(field, operator, value, custom_field_id) |
|
692 | 692 | db_table = CustomValue.table_name |
|
693 | 693 | db_field = 'value' |
|
694 | 694 | filter = @available_filters[field] |
|
695 | 695 | if filter && filter[:format] == 'user' |
|
696 | 696 | if value.delete('me') |
|
697 | 697 | value.push User.current.id.to_s |
|
698 | 698 | end |
|
699 | 699 | end |
|
700 | 700 | not_in = nil |
|
701 | 701 | if operator == '!' |
|
702 | 702 | # Makes ! operator work for custom fields with multiple values |
|
703 | 703 | operator = '=' |
|
704 | 704 | not_in = 'NOT' |
|
705 | 705 | end |
|
706 | 706 | "#{Issue.table_name}.id #{not_in} IN (SELECT #{Issue.table_name}.id FROM #{Issue.table_name} LEFT OUTER JOIN #{db_table} ON #{db_table}.customized_type='Issue' AND #{db_table}.customized_id=#{Issue.table_name}.id AND #{db_table}.custom_field_id=#{custom_field_id} WHERE " + |
|
707 | 707 | sql_for_field(field, operator, value, db_table, db_field, true) + ')' |
|
708 | 708 | end |
|
709 | 709 | |
|
710 | 710 | # Helper method to generate the WHERE sql for a +field+, +operator+ and a +value+ |
|
711 | 711 | def sql_for_field(field, operator, value, db_table, db_field, is_custom_filter=false) |
|
712 | 712 | sql = '' |
|
713 | 713 | case operator |
|
714 | 714 | when "=" |
|
715 | 715 | if value.any? |
|
716 | 716 | case type_for(field) |
|
717 | 717 | when :date, :date_past |
|
718 | 718 | sql = date_clause(db_table, db_field, (Date.parse(value.first) rescue nil), (Date.parse(value.first) rescue nil)) |
|
719 | 719 | when :integer |
|
720 | 720 | if is_custom_filter |
|
721 | 721 | sql = "(#{db_table}.#{db_field} <> '' AND CAST(#{db_table}.#{db_field} AS decimal(60,3)) = #{value.first.to_i})" |
|
722 | 722 | else |
|
723 | 723 | sql = "#{db_table}.#{db_field} = #{value.first.to_i}" |
|
724 | 724 | end |
|
725 | 725 | when :float |
|
726 | 726 | if is_custom_filter |
|
727 | 727 | 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})" |
|
728 | 728 | else |
|
729 | 729 | sql = "#{db_table}.#{db_field} BETWEEN #{value.first.to_f - 1e-5} AND #{value.first.to_f + 1e-5}" |
|
730 | 730 | end |
|
731 | 731 | else |
|
732 | 732 | sql = "#{db_table}.#{db_field} IN (" + value.collect{|val| "'#{connection.quote_string(val)}'"}.join(",") + ")" |
|
733 | 733 | end |
|
734 | 734 | else |
|
735 | 735 | # IN an empty set |
|
736 | 736 | sql = "1=0" |
|
737 | 737 | end |
|
738 | 738 | when "!" |
|
739 | 739 | if value.any? |
|
740 | 740 | sql = "(#{db_table}.#{db_field} IS NULL OR #{db_table}.#{db_field} NOT IN (" + value.collect{|val| "'#{connection.quote_string(val)}'"}.join(",") + "))" |
|
741 | 741 | else |
|
742 | 742 | # NOT IN an empty set |
|
743 | 743 | sql = "1=1" |
|
744 | 744 | end |
|
745 | 745 | when "!*" |
|
746 | 746 | sql = "#{db_table}.#{db_field} IS NULL" |
|
747 | 747 | sql << " OR #{db_table}.#{db_field} = ''" if is_custom_filter |
|
748 | 748 | when "*" |
|
749 | 749 | sql = "#{db_table}.#{db_field} IS NOT NULL" |
|
750 | 750 | sql << " AND #{db_table}.#{db_field} <> ''" if is_custom_filter |
|
751 | 751 | when ">=" |
|
752 | 752 | if [:date, :date_past].include?(type_for(field)) |
|
753 | 753 | sql = date_clause(db_table, db_field, (Date.parse(value.first) rescue nil), nil) |
|
754 | 754 | else |
|
755 | 755 | if is_custom_filter |
|
756 | 756 | sql = "(#{db_table}.#{db_field} <> '' AND CAST(#{db_table}.#{db_field} AS decimal(60,3)) >= #{value.first.to_f})" |
|
757 | 757 | else |
|
758 | 758 | sql = "#{db_table}.#{db_field} >= #{value.first.to_f}" |
|
759 | 759 | end |
|
760 | 760 | end |
|
761 | 761 | when "<=" |
|
762 | 762 | if [:date, :date_past].include?(type_for(field)) |
|
763 | 763 | sql = date_clause(db_table, db_field, nil, (Date.parse(value.first) rescue nil)) |
|
764 | 764 | else |
|
765 | 765 | if is_custom_filter |
|
766 | 766 | sql = "(#{db_table}.#{db_field} <> '' AND CAST(#{db_table}.#{db_field} AS decimal(60,3)) <= #{value.first.to_f})" |
|
767 | 767 | else |
|
768 | 768 | sql = "#{db_table}.#{db_field} <= #{value.first.to_f}" |
|
769 | 769 | end |
|
770 | 770 | end |
|
771 | 771 | when "><" |
|
772 | 772 | if [:date, :date_past].include?(type_for(field)) |
|
773 | 773 | sql = date_clause(db_table, db_field, (Date.parse(value[0]) rescue nil), (Date.parse(value[1]) rescue nil)) |
|
774 | 774 | else |
|
775 | 775 | if is_custom_filter |
|
776 | 776 | 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})" |
|
777 | 777 | else |
|
778 | 778 | sql = "#{db_table}.#{db_field} BETWEEN #{value[0].to_f} AND #{value[1].to_f}" |
|
779 | 779 | end |
|
780 | 780 | end |
|
781 | 781 | when "o" |
|
782 | 782 | sql = "#{Issue.table_name}.status_id IN (SELECT id FROM #{IssueStatus.table_name} WHERE is_closed=#{connection.quoted_false})" if field == "status_id" |
|
783 | 783 | when "c" |
|
784 | 784 | sql = "#{Issue.table_name}.status_id IN (SELECT id FROM #{IssueStatus.table_name} WHERE is_closed=#{connection.quoted_true})" if field == "status_id" |
|
785 | 785 | when ">t-" |
|
786 | 786 | sql = relative_date_clause(db_table, db_field, - value.first.to_i, 0) |
|
787 | 787 | when "<t-" |
|
788 | 788 | sql = relative_date_clause(db_table, db_field, nil, - value.first.to_i) |
|
789 | 789 | when "t-" |
|
790 | 790 | sql = relative_date_clause(db_table, db_field, - value.first.to_i, - value.first.to_i) |
|
791 | 791 | when ">t+" |
|
792 | 792 | sql = relative_date_clause(db_table, db_field, value.first.to_i, nil) |
|
793 | 793 | when "<t+" |
|
794 | 794 | sql = relative_date_clause(db_table, db_field, 0, value.first.to_i) |
|
795 | 795 | when "t+" |
|
796 | 796 | sql = relative_date_clause(db_table, db_field, value.first.to_i, value.first.to_i) |
|
797 | 797 | when "t" |
|
798 | 798 | sql = relative_date_clause(db_table, db_field, 0, 0) |
|
799 | 799 | when "w" |
|
800 | 800 | first_day_of_week = l(:general_first_day_of_week).to_i |
|
801 | 801 | day_of_week = Date.today.cwday |
|
802 | 802 | days_ago = (day_of_week >= first_day_of_week ? day_of_week - first_day_of_week : day_of_week + 7 - first_day_of_week) |
|
803 | 803 | sql = relative_date_clause(db_table, db_field, - days_ago, - days_ago + 6) |
|
804 | 804 | when "~" |
|
805 | 805 | sql = "LOWER(#{db_table}.#{db_field}) LIKE '%#{connection.quote_string(value.first.to_s.downcase)}%'" |
|
806 | 806 | when "!~" |
|
807 | 807 | sql = "LOWER(#{db_table}.#{db_field}) NOT LIKE '%#{connection.quote_string(value.first.to_s.downcase)}%'" |
|
808 | 808 | else |
|
809 | 809 | raise "Unknown query operator #{operator}" |
|
810 | 810 | end |
|
811 | 811 | |
|
812 | 812 | return sql |
|
813 | 813 | end |
|
814 | 814 | |
|
815 | 815 | def add_custom_fields_filters(custom_fields) |
|
816 | 816 | @available_filters ||= {} |
|
817 | 817 | |
|
818 | 818 | custom_fields.select(&:is_filter?).each do |field| |
|
819 | 819 | case field.field_format |
|
820 | 820 | when "text" |
|
821 | 821 | options = { :type => :text, :order => 20 } |
|
822 | 822 | when "list" |
|
823 | 823 | options = { :type => :list_optional, :values => field.possible_values, :order => 20} |
|
824 | 824 | when "date" |
|
825 | 825 | options = { :type => :date, :order => 20 } |
|
826 | 826 | when "bool" |
|
827 | 827 | options = { :type => :list, :values => [[l(:general_text_yes), "1"], [l(:general_text_no), "0"]], :order => 20 } |
|
828 | 828 | when "int" |
|
829 | 829 | options = { :type => :integer, :order => 20 } |
|
830 | 830 | when "float" |
|
831 | 831 | options = { :type => :float, :order => 20 } |
|
832 | 832 | when "user", "version" |
|
833 | 833 | next unless project |
|
834 | 834 | values = field.possible_values_options(project) |
|
835 | 835 | if User.current.logged? && field.field_format == 'user' |
|
836 | 836 | values.unshift ["<< #{l(:label_me)} >>", "me"] |
|
837 | 837 | end |
|
838 | 838 | options = { :type => :list_optional, :values => values, :order => 20} |
|
839 | 839 | else |
|
840 | 840 | options = { :type => :string, :order => 20 } |
|
841 | 841 | end |
|
842 | 842 | @available_filters["cf_#{field.id}"] = options.merge({ :name => field.name, :format => field.field_format }) |
|
843 | 843 | end |
|
844 | 844 | end |
|
845 | 845 | |
|
846 | 846 | # Returns a SQL clause for a date or datetime field. |
|
847 | 847 | def date_clause(table, field, from, to) |
|
848 | 848 | s = [] |
|
849 | 849 | if from |
|
850 | 850 | from_yesterday = from - 1 |
|
851 | 851 | from_yesterday_utc = Time.gm(from_yesterday.year, from_yesterday.month, from_yesterday.day) |
|
852 | 852 | s << ("#{table}.#{field} > '%s'" % [connection.quoted_date(from_yesterday_utc.end_of_day)]) |
|
853 | 853 | end |
|
854 | 854 | if to |
|
855 | 855 | to_utc = Time.gm(to.year, to.month, to.day) |
|
856 | 856 | s << ("#{table}.#{field} <= '%s'" % [connection.quoted_date(to_utc.end_of_day)]) |
|
857 | 857 | end |
|
858 | 858 | s.join(' AND ') |
|
859 | 859 | end |
|
860 | 860 | |
|
861 | 861 | # Returns a SQL clause for a date or datetime field using relative dates. |
|
862 | 862 | def relative_date_clause(table, field, days_from, days_to) |
|
863 | 863 | date_clause(table, field, (days_from ? Date.today + days_from : nil), (days_to ? Date.today + days_to : nil)) |
|
864 | 864 | end |
|
865 | 865 | end |
General Comments 0
You need to be logged in to leave comments.
Login now