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