@@ -1,257 +1,265 | |||||
1 | # redMine - project management software |
|
1 | # redMine - project management software | |
2 | # Copyright (C) 2006-2007 Jean-Philippe Lang |
|
2 | # Copyright (C) 2006-2007 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 Query < ActiveRecord::Base |
|
18 | class Query < ActiveRecord::Base | |
19 | belongs_to :project |
|
19 | belongs_to :project | |
20 | belongs_to :user |
|
20 | belongs_to :user | |
21 | serialize :filters |
|
21 | serialize :filters | |
22 |
|
22 | |||
23 | attr_protected :project, :user |
|
23 | attr_protected :project, :user | |
24 | attr_accessor :executed_by |
|
24 | attr_accessor :executed_by | |
25 |
|
25 | |||
26 | validates_presence_of :name, :on => :save |
|
26 | validates_presence_of :name, :on => :save | |
27 | validates_length_of :name, :maximum => 255 |
|
27 | validates_length_of :name, :maximum => 255 | |
28 |
|
28 | |||
29 | @@operators = { "=" => :label_equals, |
|
29 | @@operators = { "=" => :label_equals, | |
30 | "!" => :label_not_equals, |
|
30 | "!" => :label_not_equals, | |
31 | "o" => :label_open_issues, |
|
31 | "o" => :label_open_issues, | |
32 | "c" => :label_closed_issues, |
|
32 | "c" => :label_closed_issues, | |
33 | "!*" => :label_none, |
|
33 | "!*" => :label_none, | |
34 | "*" => :label_all, |
|
34 | "*" => :label_all, | |
|
35 | ">=" => '>=', | |||
|
36 | "<=" => '<=', | |||
35 | "<t+" => :label_in_less_than, |
|
37 | "<t+" => :label_in_less_than, | |
36 | ">t+" => :label_in_more_than, |
|
38 | ">t+" => :label_in_more_than, | |
37 | "t+" => :label_in, |
|
39 | "t+" => :label_in, | |
38 | "t" => :label_today, |
|
40 | "t" => :label_today, | |
39 | "w" => :label_this_week, |
|
41 | "w" => :label_this_week, | |
40 | ">t-" => :label_less_than_ago, |
|
42 | ">t-" => :label_less_than_ago, | |
41 | "<t-" => :label_more_than_ago, |
|
43 | "<t-" => :label_more_than_ago, | |
42 | "t-" => :label_ago, |
|
44 | "t-" => :label_ago, | |
43 | "~" => :label_contains, |
|
45 | "~" => :label_contains, | |
44 | "!~" => :label_not_contains } |
|
46 | "!~" => :label_not_contains } | |
45 |
|
47 | |||
46 | cattr_reader :operators |
|
48 | cattr_reader :operators | |
47 |
|
49 | |||
48 | @@operators_by_filter_type = { :list => [ "=", "!" ], |
|
50 | @@operators_by_filter_type = { :list => [ "=", "!" ], | |
49 | :list_status => [ "o", "=", "!", "c", "*" ], |
|
51 | :list_status => [ "o", "=", "!", "c", "*" ], | |
50 | :list_optional => [ "=", "!", "!*", "*" ], |
|
52 | :list_optional => [ "=", "!", "!*", "*" ], | |
51 | :list_one_or_more => [ "*", "=" ], |
|
53 | :list_one_or_more => [ "*", "=" ], | |
52 | :date => [ "<t+", ">t+", "t+", "t", "w", ">t-", "<t-", "t-" ], |
|
54 | :date => [ "<t+", ">t+", "t+", "t", "w", ">t-", "<t-", "t-" ], | |
53 | :date_past => [ ">t-", "<t-", "t-", "t", "w" ], |
|
55 | :date_past => [ ">t-", "<t-", "t-", "t", "w" ], | |
54 | :string => [ "=", "~", "!", "!~" ], |
|
56 | :string => [ "=", "~", "!", "!~" ], | |
55 |
:text => [ "~", "!~" ] |
|
57 | :text => [ "~", "!~" ], | |
|
58 | :integer => [ "=", ">=", "<=" ] } | |||
56 |
|
59 | |||
57 | cattr_reader :operators_by_filter_type |
|
60 | cattr_reader :operators_by_filter_type | |
58 |
|
61 | |||
59 | def initialize(attributes = nil) |
|
62 | def initialize(attributes = nil) | |
60 | super attributes |
|
63 | super attributes | |
61 | self.filters ||= { 'status_id' => {:operator => "o", :values => [""]} } |
|
64 | self.filters ||= { 'status_id' => {:operator => "o", :values => [""]} } | |
62 | end |
|
65 | end | |
63 |
|
66 | |||
64 | def executed_by=(user) |
|
67 | def executed_by=(user) | |
65 | @executed_by = user |
|
68 | @executed_by = user | |
66 | set_language_if_valid(user.language) if user |
|
69 | set_language_if_valid(user.language) if user | |
67 | end |
|
70 | end | |
68 |
|
71 | |||
69 | def validate |
|
72 | def validate | |
70 | filters.each_key do |field| |
|
73 | filters.each_key do |field| | |
71 | errors.add label_for(field), :activerecord_error_blank unless |
|
74 | errors.add label_for(field), :activerecord_error_blank unless | |
72 | # filter requires one or more values |
|
75 | # filter requires one or more values | |
73 | (values_for(field) and !values_for(field).first.empty?) or |
|
76 | (values_for(field) and !values_for(field).first.empty?) or | |
74 | # filter doesn't require any value |
|
77 | # filter doesn't require any value | |
75 | ["o", "c", "!*", "*", "t", "w"].include? operator_for(field) |
|
78 | ["o", "c", "!*", "*", "t", "w"].include? operator_for(field) | |
76 | end if filters |
|
79 | end if filters | |
77 | end |
|
80 | end | |
78 |
|
81 | |||
79 | def editable_by?(user) |
|
82 | def editable_by?(user) | |
80 | return false unless user |
|
83 | return false unless user | |
81 | return true if !is_public && self.user_id == user.id |
|
84 | return true if !is_public && self.user_id == user.id | |
82 | is_public && user.allowed_to?(:manage_pulic_queries, project) |
|
85 | is_public && user.allowed_to?(:manage_pulic_queries, project) | |
83 | end |
|
86 | end | |
84 |
|
87 | |||
85 | def available_filters |
|
88 | def available_filters | |
86 | return @available_filters if @available_filters |
|
89 | return @available_filters if @available_filters | |
87 | @available_filters = { "status_id" => { :type => :list_status, :order => 1, :values => IssueStatus.find(:all, :order => 'position').collect{|s| [s.name, s.id.to_s] } }, |
|
90 | @available_filters = { "status_id" => { :type => :list_status, :order => 1, :values => IssueStatus.find(:all, :order => 'position').collect{|s| [s.name, s.id.to_s] } }, | |
88 | "tracker_id" => { :type => :list, :order => 2, :values => Tracker.find(:all, :order => 'position').collect{|s| [s.name, s.id.to_s] } }, |
|
91 | "tracker_id" => { :type => :list, :order => 2, :values => Tracker.find(:all, :order => 'position').collect{|s| [s.name, s.id.to_s] } }, | |
89 | "priority_id" => { :type => :list, :order => 3, :values => Enumeration.find(:all, :conditions => ['opt=?','IPRI']).collect{|s| [s.name, s.id.to_s] } }, |
|
92 | "priority_id" => { :type => :list, :order => 3, :values => Enumeration.find(:all, :conditions => ['opt=?','IPRI']).collect{|s| [s.name, s.id.to_s] } }, | |
90 | "subject" => { :type => :text, :order => 8 }, |
|
93 | "subject" => { :type => :text, :order => 8 }, | |
91 | "created_on" => { :type => :date_past, :order => 9 }, |
|
94 | "created_on" => { :type => :date_past, :order => 9 }, | |
92 | "updated_on" => { :type => :date_past, :order => 10 }, |
|
95 | "updated_on" => { :type => :date_past, :order => 10 }, | |
93 | "start_date" => { :type => :date, :order => 11 }, |
|
96 | "start_date" => { :type => :date, :order => 11 }, | |
94 |
"due_date" => { :type => :date, :order => 12 } |
|
97 | "due_date" => { :type => :date, :order => 12 }, | |
|
98 | "done_ratio" => { :type => :integer, :order => 13 }} | |||
95 |
|
99 | |||
96 | user_values = [] |
|
100 | user_values = [] | |
97 | if project |
|
101 | if project | |
98 | user_values += project.users.collect{|s| [s.name, s.id.to_s] } |
|
102 | user_values += project.users.collect{|s| [s.name, s.id.to_s] } | |
99 | elsif executed_by |
|
103 | elsif executed_by | |
100 | user_values << ["<< #{l(:label_me)} >>", "me"] if executed_by |
|
104 | user_values << ["<< #{l(:label_me)} >>", "me"] if executed_by | |
101 | # members of the user's projects |
|
105 | # members of the user's projects | |
102 | user_values += executed_by.projects.collect(&:users).flatten.uniq.sort.collect{|s| [s.name, s.id.to_s] } |
|
106 | user_values += executed_by.projects.collect(&:users).flatten.uniq.sort.collect{|s| [s.name, s.id.to_s] } | |
103 | end |
|
107 | end | |
104 | @available_filters["assigned_to_id"] = { :type => :list_optional, :order => 4, :values => user_values } unless user_values.empty? |
|
108 | @available_filters["assigned_to_id"] = { :type => :list_optional, :order => 4, :values => user_values } unless user_values.empty? | |
105 | @available_filters["author_id"] = { :type => :list, :order => 5, :values => user_values } unless user_values.empty? |
|
109 | @available_filters["author_id"] = { :type => :list, :order => 5, :values => user_values } unless user_values.empty? | |
106 |
|
110 | |||
107 | if project |
|
111 | if project | |
108 | # project specific filters |
|
112 | # project specific filters | |
109 | @available_filters["category_id"] = { :type => :list_optional, :order => 6, :values => @project.issue_categories.collect{|s| [s.name, s.id.to_s] } } |
|
113 | @available_filters["category_id"] = { :type => :list_optional, :order => 6, :values => @project.issue_categories.collect{|s| [s.name, s.id.to_s] } } | |
110 | @available_filters["fixed_version_id"] = { :type => :list_optional, :order => 7, :values => @project.versions.sort.collect{|s| [s.name, s.id.to_s] } } |
|
114 | @available_filters["fixed_version_id"] = { :type => :list_optional, :order => 7, :values => @project.versions.sort.collect{|s| [s.name, s.id.to_s] } } | |
111 | unless @project.active_children.empty? |
|
115 | unless @project.active_children.empty? | |
112 | @available_filters["subproject_id"] = { :type => :list_one_or_more, :order => 13, :values => @project.active_children.collect{|s| [s.name, s.id.to_s] } } |
|
116 | @available_filters["subproject_id"] = { :type => :list_one_or_more, :order => 13, :values => @project.active_children.collect{|s| [s.name, s.id.to_s] } } | |
113 | end |
|
117 | end | |
114 | @project.all_custom_fields.select(&:is_filter?).each do |field| |
|
118 | @project.all_custom_fields.select(&:is_filter?).each do |field| | |
115 | case field.field_format |
|
119 | case field.field_format | |
116 | when "string", "int" |
|
120 | when "string", "int" | |
117 | options = { :type => :string, :order => 20 } |
|
121 | options = { :type => :string, :order => 20 } | |
118 | when "text" |
|
122 | when "text" | |
119 | options = { :type => :text, :order => 20 } |
|
123 | options = { :type => :text, :order => 20 } | |
120 | when "list" |
|
124 | when "list" | |
121 | options = { :type => :list_optional, :values => field.possible_values, :order => 20} |
|
125 | options = { :type => :list_optional, :values => field.possible_values, :order => 20} | |
122 | when "date" |
|
126 | when "date" | |
123 | options = { :type => :date, :order => 20 } |
|
127 | options = { :type => :date, :order => 20 } | |
124 | when "bool" |
|
128 | when "bool" | |
125 | options = { :type => :list, :values => [[l(:general_text_yes), "1"], [l(:general_text_no), "0"]], :order => 20 } |
|
129 | options = { :type => :list, :values => [[l(:general_text_yes), "1"], [l(:general_text_no), "0"]], :order => 20 } | |
126 | end |
|
130 | end | |
127 | @available_filters["cf_#{field.id}"] = options.merge({ :name => field.name }) |
|
131 | @available_filters["cf_#{field.id}"] = options.merge({ :name => field.name }) | |
128 | end |
|
132 | end | |
129 | # remove category filter if no category defined |
|
133 | # remove category filter if no category defined | |
130 | @available_filters.delete "category_id" if @available_filters["category_id"][:values].empty? |
|
134 | @available_filters.delete "category_id" if @available_filters["category_id"][:values].empty? | |
131 | end |
|
135 | end | |
132 | @available_filters |
|
136 | @available_filters | |
133 | end |
|
137 | end | |
134 |
|
138 | |||
135 | def add_filter(field, operator, values) |
|
139 | def add_filter(field, operator, values) | |
136 | # values must be an array |
|
140 | # values must be an array | |
137 | return unless values and values.is_a? Array # and !values.first.empty? |
|
141 | return unless values and values.is_a? Array # and !values.first.empty? | |
138 | # check if field is defined as an available filter |
|
142 | # check if field is defined as an available filter | |
139 | if available_filters.has_key? field |
|
143 | if available_filters.has_key? field | |
140 | filter_options = available_filters[field] |
|
144 | filter_options = available_filters[field] | |
141 | # check if operator is allowed for that filter |
|
145 | # check if operator is allowed for that filter | |
142 | #if @@operators_by_filter_type[filter_options[:type]].include? operator |
|
146 | #if @@operators_by_filter_type[filter_options[:type]].include? operator | |
143 | # allowed_values = values & ([""] + (filter_options[:values] || []).collect {|val| val[1]}) |
|
147 | # allowed_values = values & ([""] + (filter_options[:values] || []).collect {|val| val[1]}) | |
144 | # filters[field] = {:operator => operator, :values => allowed_values } if (allowed_values.first and !allowed_values.first.empty?) or ["o", "c", "!*", "*", "t"].include? operator |
|
148 | # filters[field] = {:operator => operator, :values => allowed_values } if (allowed_values.first and !allowed_values.first.empty?) or ["o", "c", "!*", "*", "t"].include? operator | |
145 | #end |
|
149 | #end | |
146 | filters[field] = {:operator => operator, :values => values } |
|
150 | filters[field] = {:operator => operator, :values => values } | |
147 | end |
|
151 | end | |
148 | end |
|
152 | end | |
149 |
|
153 | |||
150 | def add_short_filter(field, expression) |
|
154 | def add_short_filter(field, expression) | |
151 | return unless expression |
|
155 | return unless expression | |
152 | parms = expression.scan(/^(o|c|\!|\*)?(.*)$/).first |
|
156 | parms = expression.scan(/^(o|c|\!|\*)?(.*)$/).first | |
153 | add_filter field, (parms[0] || "="), [parms[1] || ""] |
|
157 | add_filter field, (parms[0] || "="), [parms[1] || ""] | |
154 | end |
|
158 | end | |
155 |
|
159 | |||
156 | def has_filter?(field) |
|
160 | def has_filter?(field) | |
157 | filters and filters[field] |
|
161 | filters and filters[field] | |
158 | end |
|
162 | end | |
159 |
|
163 | |||
160 | def operator_for(field) |
|
164 | def operator_for(field) | |
161 | has_filter?(field) ? filters[field][:operator] : nil |
|
165 | has_filter?(field) ? filters[field][:operator] : nil | |
162 | end |
|
166 | end | |
163 |
|
167 | |||
164 | def values_for(field) |
|
168 | def values_for(field) | |
165 | has_filter?(field) ? filters[field][:values] : nil |
|
169 | has_filter?(field) ? filters[field][:values] : nil | |
166 | end |
|
170 | end | |
167 |
|
171 | |||
168 | def label_for(field) |
|
172 | def label_for(field) | |
169 | label = @available_filters[field][:name] if @available_filters.has_key?(field) |
|
173 | label = @available_filters[field][:name] if @available_filters.has_key?(field) | |
170 | label ||= field.gsub(/\_id$/, "") |
|
174 | label ||= field.gsub(/\_id$/, "") | |
171 | end |
|
175 | end | |
172 |
|
176 | |||
173 | def statement |
|
177 | def statement | |
174 | # project/subprojects clause |
|
178 | # project/subprojects clause | |
175 | clause = '' |
|
179 | clause = '' | |
176 | if project && has_filter?("subproject_id") |
|
180 | if project && has_filter?("subproject_id") | |
177 | subproject_ids = [] |
|
181 | subproject_ids = [] | |
178 | if operator_for("subproject_id") == "=" |
|
182 | if operator_for("subproject_id") == "=" | |
179 | subproject_ids = values_for("subproject_id").each(&:to_i) |
|
183 | subproject_ids = values_for("subproject_id").each(&:to_i) | |
180 | else |
|
184 | else | |
181 | subproject_ids = project.active_children.collect{|p| p.id} |
|
185 | subproject_ids = project.active_children.collect{|p| p.id} | |
182 | end |
|
186 | end | |
183 | clause << "#{Issue.table_name}.project_id IN (%d,%s)" % [project.id, subproject_ids.join(",")] if project |
|
187 | clause << "#{Issue.table_name}.project_id IN (%d,%s)" % [project.id, subproject_ids.join(",")] if project | |
184 | elsif project |
|
188 | elsif project | |
185 | clause << "#{Issue.table_name}.project_id=%d" % project.id |
|
189 | clause << "#{Issue.table_name}.project_id=%d" % project.id | |
186 | else |
|
190 | else | |
187 | clause << Project.visible_by(executed_by) |
|
191 | clause << Project.visible_by(executed_by) | |
188 | end |
|
192 | end | |
189 |
|
193 | |||
190 | # filters clauses |
|
194 | # filters clauses | |
191 | filters_clauses = [] |
|
195 | filters_clauses = [] | |
192 | filters.each_key do |field| |
|
196 | filters.each_key do |field| | |
193 | next if field == "subproject_id" |
|
197 | next if field == "subproject_id" | |
194 | v = values_for(field).clone |
|
198 | v = values_for(field).clone | |
195 | next unless v and !v.empty? |
|
199 | next unless v and !v.empty? | |
196 |
|
200 | |||
197 | sql = '' |
|
201 | sql = '' | |
198 | if field =~ /^cf_(\d+)$/ |
|
202 | if field =~ /^cf_(\d+)$/ | |
199 | # custom field |
|
203 | # custom field | |
200 | db_table = CustomValue.table_name |
|
204 | db_table = CustomValue.table_name | |
201 | db_field = 'value' |
|
205 | db_field = 'value' | |
202 | sql << "#{Issue.table_name}.id IN (SELECT #{db_table}.customized_id FROM #{db_table} where #{db_table}.customized_type='Issue' AND #{db_table}.customized_id=#{Issue.table_name}.id AND #{db_table}.custom_field_id=#{$1} AND " |
|
206 | sql << "#{Issue.table_name}.id IN (SELECT #{db_table}.customized_id FROM #{db_table} where #{db_table}.customized_type='Issue' AND #{db_table}.customized_id=#{Issue.table_name}.id AND #{db_table}.custom_field_id=#{$1} AND " | |
203 | else |
|
207 | else | |
204 | # regular field |
|
208 | # regular field | |
205 | db_table = Issue.table_name |
|
209 | db_table = Issue.table_name | |
206 | db_field = field |
|
210 | db_field = field | |
207 | sql << '(' |
|
211 | sql << '(' | |
208 | end |
|
212 | end | |
209 |
|
213 | |||
210 | # "me" value subsitution |
|
214 | # "me" value subsitution | |
211 | if %w(assigned_to_id author_id).include?(field) |
|
215 | if %w(assigned_to_id author_id).include?(field) | |
212 | v.push(executed_by ? executed_by.id.to_s : "0") if v.delete("me") |
|
216 | v.push(executed_by ? executed_by.id.to_s : "0") if v.delete("me") | |
213 | end |
|
217 | end | |
214 |
|
218 | |||
215 | case operator_for field |
|
219 | case operator_for field | |
216 | when "=" |
|
220 | when "=" | |
217 | sql = sql + "#{db_table}.#{db_field} IN (" + v.collect{|val| "'#{connection.quote_string(val)}'"}.join(",") + ")" |
|
221 | sql = sql + "#{db_table}.#{db_field} IN (" + v.collect{|val| "'#{connection.quote_string(val)}'"}.join(",") + ")" | |
218 | when "!" |
|
222 | when "!" | |
219 | sql = sql + "#{db_table}.#{db_field} NOT IN (" + v.collect{|val| "'#{connection.quote_string(val)}'"}.join(",") + ")" |
|
223 | sql = sql + "#{db_table}.#{db_field} NOT IN (" + v.collect{|val| "'#{connection.quote_string(val)}'"}.join(",") + ")" | |
220 | when "!*" |
|
224 | when "!*" | |
221 | sql = sql + "#{db_table}.#{db_field} IS NULL" |
|
225 | sql = sql + "#{db_table}.#{db_field} IS NULL" | |
222 | when "*" |
|
226 | when "*" | |
223 | sql = sql + "#{db_table}.#{db_field} IS NOT NULL" |
|
227 | sql = sql + "#{db_table}.#{db_field} IS NOT NULL" | |
|
228 | when ">=" | |||
|
229 | sql = sql + "#{db_table}.#{db_field} >= #{v.first.to_i}" | |||
|
230 | when "<=" | |||
|
231 | sql = sql + "#{db_table}.#{db_field} <= #{v.first.to_i}" | |||
224 | when "o" |
|
232 | when "o" | |
225 | sql = sql + "#{IssueStatus.table_name}.is_closed=#{connection.quoted_false}" if field == "status_id" |
|
233 | sql = sql + "#{IssueStatus.table_name}.is_closed=#{connection.quoted_false}" if field == "status_id" | |
226 | when "c" |
|
234 | when "c" | |
227 | sql = sql + "#{IssueStatus.table_name}.is_closed=#{connection.quoted_true}" if field == "status_id" |
|
235 | sql = sql + "#{IssueStatus.table_name}.is_closed=#{connection.quoted_true}" if field == "status_id" | |
228 | when ">t-" |
|
236 | when ">t-" | |
229 | sql = sql + "#{db_table}.#{db_field} BETWEEN '%s' AND '%s'" % [connection.quoted_date((Date.today - v.first.to_i).to_time), connection.quoted_date((Date.today + 1).to_time)] |
|
237 | sql = sql + "#{db_table}.#{db_field} BETWEEN '%s' AND '%s'" % [connection.quoted_date((Date.today - v.first.to_i).to_time), connection.quoted_date((Date.today + 1).to_time)] | |
230 | when "<t-" |
|
238 | when "<t-" | |
231 | sql = sql + "#{db_table}.#{db_field} <= '%s'" % connection.quoted_date((Date.today - v.first.to_i).to_time) |
|
239 | sql = sql + "#{db_table}.#{db_field} <= '%s'" % connection.quoted_date((Date.today - v.first.to_i).to_time) | |
232 | when "t-" |
|
240 | when "t-" | |
233 | sql = sql + "#{db_table}.#{db_field} BETWEEN '%s' AND '%s'" % [connection.quoted_date((Date.today - v.first.to_i).to_time), connection.quoted_date((Date.today - v.first.to_i + 1).to_time)] |
|
241 | sql = sql + "#{db_table}.#{db_field} BETWEEN '%s' AND '%s'" % [connection.quoted_date((Date.today - v.first.to_i).to_time), connection.quoted_date((Date.today - v.first.to_i + 1).to_time)] | |
234 | when ">t+" |
|
242 | when ">t+" | |
235 | sql = sql + "#{db_table}.#{db_field} >= '%s'" % connection.quoted_date((Date.today + v.first.to_i).to_time) |
|
243 | sql = sql + "#{db_table}.#{db_field} >= '%s'" % connection.quoted_date((Date.today + v.first.to_i).to_time) | |
236 | when "<t+" |
|
244 | when "<t+" | |
237 | sql = sql + "#{db_table}.#{db_field} BETWEEN '%s' AND '%s'" % [connection.quoted_date(Date.today.to_time), connection.quoted_date((Date.today + v.first.to_i + 1).to_time)] |
|
245 | sql = sql + "#{db_table}.#{db_field} BETWEEN '%s' AND '%s'" % [connection.quoted_date(Date.today.to_time), connection.quoted_date((Date.today + v.first.to_i + 1).to_time)] | |
238 | when "t+" |
|
246 | when "t+" | |
239 | sql = sql + "#{db_table}.#{db_field} BETWEEN '%s' AND '%s'" % [connection.quoted_date((Date.today + v.first.to_i).to_time), connection.quoted_date((Date.today + v.first.to_i + 1).to_time)] |
|
247 | sql = sql + "#{db_table}.#{db_field} BETWEEN '%s' AND '%s'" % [connection.quoted_date((Date.today + v.first.to_i).to_time), connection.quoted_date((Date.today + v.first.to_i + 1).to_time)] | |
240 | when "t" |
|
248 | when "t" | |
241 | sql = sql + "#{db_table}.#{db_field} BETWEEN '%s' AND '%s'" % [connection.quoted_date(Date.today.to_time), connection.quoted_date((Date.today+1).to_time)] |
|
249 | sql = sql + "#{db_table}.#{db_field} BETWEEN '%s' AND '%s'" % [connection.quoted_date(Date.today.to_time), connection.quoted_date((Date.today+1).to_time)] | |
242 | when "w" |
|
250 | when "w" | |
243 | sql = sql + "#{db_table}.#{db_field} BETWEEN '%s' AND '%s'" % [connection.quoted_date(Time.now.at_beginning_of_week), connection.quoted_date(Time.now.next_week.yesterday)] |
|
251 | sql = sql + "#{db_table}.#{db_field} BETWEEN '%s' AND '%s'" % [connection.quoted_date(Time.now.at_beginning_of_week), connection.quoted_date(Time.now.next_week.yesterday)] | |
244 | when "~" |
|
252 | when "~" | |
245 | sql = sql + "#{db_table}.#{db_field} LIKE '%#{connection.quote_string(v.first)}%'" |
|
253 | sql = sql + "#{db_table}.#{db_field} LIKE '%#{connection.quote_string(v.first)}%'" | |
246 | when "!~" |
|
254 | when "!~" | |
247 | sql = sql + "#{db_table}.#{db_field} NOT LIKE '%#{connection.quote_string(v.first)}%'" |
|
255 | sql = sql + "#{db_table}.#{db_field} NOT LIKE '%#{connection.quote_string(v.first)}%'" | |
248 | end |
|
256 | end | |
249 | sql << ')' |
|
257 | sql << ')' | |
250 | filters_clauses << sql |
|
258 | filters_clauses << sql | |
251 | end if filters and valid? |
|
259 | end if filters and valid? | |
252 |
|
260 | |||
253 | clause << ' AND ' unless clause.empty? |
|
261 | clause << ' AND ' unless clause.empty? | |
254 | clause << filters_clauses.join(' AND ') unless filters_clauses.empty? |
|
262 | clause << filters_clauses.join(' AND ') unless filters_clauses.empty? | |
255 | clause |
|
263 | clause | |
256 | end |
|
264 | end | |
257 | end |
|
265 | end |
@@ -1,101 +1,103 | |||||
1 | <script type="text/javascript"> |
|
1 | <script type="text/javascript"> | |
2 | //<![CDATA[ |
|
2 | //<![CDATA[ | |
3 | function add_filter() { |
|
3 | function add_filter() { | |
4 | select = $('add_filter_select'); |
|
4 | select = $('add_filter_select'); | |
5 | field = select.value |
|
5 | field = select.value | |
6 | Element.show('tr_' + field); |
|
6 | Element.show('tr_' + field); | |
7 | check_box = $('cb_' + field); |
|
7 | check_box = $('cb_' + field); | |
8 | check_box.checked = true; |
|
8 | check_box.checked = true; | |
9 | toggle_filter(field); |
|
9 | toggle_filter(field); | |
10 | select.selectedIndex = 0; |
|
10 | select.selectedIndex = 0; | |
11 |
|
11 | |||
12 | for (i=0; i<select.options.length; i++) { |
|
12 | for (i=0; i<select.options.length; i++) { | |
13 | if (select.options[i].value == field) { |
|
13 | if (select.options[i].value == field) { | |
14 | select.options[i].disabled = true; |
|
14 | select.options[i].disabled = true; | |
15 | } |
|
15 | } | |
16 | } |
|
16 | } | |
17 | } |
|
17 | } | |
18 |
|
18 | |||
19 | function toggle_filter(field) { |
|
19 | function toggle_filter(field) { | |
20 | check_box = $('cb_' + field); |
|
20 | check_box = $('cb_' + field); | |
21 |
|
21 | |||
22 | if (check_box.checked) { |
|
22 | if (check_box.checked) { | |
23 | Element.show("operators_" + field); |
|
23 | Element.show("operators_" + field); | |
24 | toggle_operator(field); |
|
24 | toggle_operator(field); | |
25 | } else { |
|
25 | } else { | |
26 | Element.hide("operators_" + field); |
|
26 | Element.hide("operators_" + field); | |
27 | Element.hide("div_values_" + field); |
|
27 | Element.hide("div_values_" + field); | |
28 | } |
|
28 | } | |
29 | } |
|
29 | } | |
30 |
|
30 | |||
31 | function toggle_operator(field) { |
|
31 | function toggle_operator(field) { | |
32 | operator = $("operators_" + field); |
|
32 | operator = $("operators_" + field); | |
33 | switch (operator.value) { |
|
33 | switch (operator.value) { | |
34 | case "!*": |
|
34 | case "!*": | |
35 | case "*": |
|
35 | case "*": | |
36 | case "t": |
|
36 | case "t": | |
37 | case "w": |
|
37 | case "w": | |
38 | case "o": |
|
38 | case "o": | |
39 | case "c": |
|
39 | case "c": | |
40 | Element.hide("div_values_" + field); |
|
40 | Element.hide("div_values_" + field); | |
41 | break; |
|
41 | break; | |
42 | default: |
|
42 | default: | |
43 | Element.show("div_values_" + field); |
|
43 | Element.show("div_values_" + field); | |
44 | break; |
|
44 | break; | |
45 | } |
|
45 | } | |
46 | } |
|
46 | } | |
47 |
|
47 | |||
48 | function toggle_multi_select(field) { |
|
48 | function toggle_multi_select(field) { | |
49 | select = $('values_' + field); |
|
49 | select = $('values_' + field); | |
50 | if (select.multiple == true) { |
|
50 | if (select.multiple == true) { | |
51 | select.multiple = false; |
|
51 | select.multiple = false; | |
52 | } else { |
|
52 | } else { | |
53 | select.multiple = true; |
|
53 | select.multiple = true; | |
54 | } |
|
54 | } | |
55 | } |
|
55 | } | |
56 | //]]> |
|
56 | //]]> | |
57 | </script> |
|
57 | </script> | |
58 |
|
58 | |||
59 | <fieldset><legend><%= l(:label_filter_plural) %></legend> |
|
59 | <fieldset><legend><%= l(:label_filter_plural) %></legend> | |
60 | <table width="100%"> |
|
60 | <table width="100%"> | |
61 | <tr> |
|
61 | <tr> | |
62 | <td> |
|
62 | <td> | |
63 | <table style="padding:0;"> |
|
63 | <table style="padding:0;"> | |
64 | <% query.available_filters.sort{|a,b| a[1][:order]<=>b[1][:order]}.each do |filter| %> |
|
64 | <% query.available_filters.sort{|a,b| a[1][:order]<=>b[1][:order]}.each do |filter| %> | |
65 | <% field = filter[0] |
|
65 | <% field = filter[0] | |
66 | options = filter[1] %> |
|
66 | options = filter[1] %> | |
67 | <tr <%= 'style="display:none;"' unless query.has_filter?(field) %> id="tr_<%= field %>"> |
|
67 | <tr <%= 'style="display:none;"' unless query.has_filter?(field) %> id="tr_<%= field %>"> | |
68 | <td valign="top" style="width:200px;"> |
|
68 | <td valign="top" style="width:200px;"> | |
69 | <%= check_box_tag 'fields[]', field, query.has_filter?(field), :onclick => "toggle_filter('#{field}');", :id => "cb_#{field}" %> |
|
69 | <%= check_box_tag 'fields[]', field, query.has_filter?(field), :onclick => "toggle_filter('#{field}');", :id => "cb_#{field}" %> | |
70 | <label for="cb_<%= field %>"><%= filter[1][:name] || l(("field_"+field.to_s.gsub(/\_id$/, "")).to_sym) %></label> |
|
70 | <label for="cb_<%= field %>"><%= filter[1][:name] || l(("field_"+field.to_s.gsub(/\_id$/, "")).to_sym) %></label> | |
71 | </td> |
|
71 | </td> | |
72 | <td valign="top" style="width:150px;"> |
|
72 | <td valign="top" style="width:150px;"> | |
73 | <%= select_tag "operators[#{field}]", options_for_select(operators_for_select(options[:type]), query.operator_for(field)), :id => "operators_#{field}", :onchange => "toggle_operator('#{field}');", :class => "select-small", :style => "vertical-align: top;" %> |
|
73 | <%= select_tag "operators[#{field}]", options_for_select(operators_for_select(options[:type]), query.operator_for(field)), :id => "operators_#{field}", :onchange => "toggle_operator('#{field}');", :class => "select-small", :style => "vertical-align: top;" %> | |
74 | </td> |
|
74 | </td> | |
75 | <td valign="top"> |
|
75 | <td valign="top"> | |
76 | <div id="div_values_<%= field %>" style="display:none;"> |
|
76 | <div id="div_values_<%= field %>" style="display:none;"> | |
77 | <% case options[:type] |
|
77 | <% case options[:type] | |
78 | when :list, :list_optional, :list_status, :list_one_or_more %> |
|
78 | when :list, :list_optional, :list_status, :list_one_or_more %> | |
79 | <select <%= "multiple=true" if query.values_for(field) and query.values_for(field).length > 1 %> name="values[<%= field %>][]" id="values_<%= field %>" class="select-small" style="vertical-align: top;"> |
|
79 | <select <%= "multiple=true" if query.values_for(field) and query.values_for(field).length > 1 %> name="values[<%= field %>][]" id="values_<%= field %>" class="select-small" style="vertical-align: top;"> | |
80 | <%= options_for_select options[:values], query.values_for(field) %> |
|
80 | <%= options_for_select options[:values], query.values_for(field) %> | |
81 | </select> |
|
81 | </select> | |
82 | <%= link_to_function image_tag('expand.png'), "toggle_multi_select('#{field}');" %> |
|
82 | <%= link_to_function image_tag('expand.png'), "toggle_multi_select('#{field}');" %> | |
83 | <% when :date, :date_past %> |
|
83 | <% when :date, :date_past %> | |
84 | <%= text_field_tag "values[#{field}][]", query.values_for(field), :id => "values_#{field}", :size => 3, :class => "select-small" %> <%= l(:label_day_plural) %> |
|
84 | <%= text_field_tag "values[#{field}][]", query.values_for(field), :id => "values_#{field}", :size => 3, :class => "select-small" %> <%= l(:label_day_plural) %> | |
85 | <% when :string, :text %> |
|
85 | <% when :string, :text %> | |
86 | <%= text_field_tag "values[#{field}][]", query.values_for(field), :id => "values_#{field}", :size => 30, :class => "select-small" %> |
|
86 | <%= text_field_tag "values[#{field}][]", query.values_for(field), :id => "values_#{field}", :size => 30, :class => "select-small" %> | |
|
87 | <% when :integer %> | |||
|
88 | <%= text_field_tag "values[#{field}][]", query.values_for(field), :id => "values_#{field}", :size => 3, :class => "select-small" %> | |||
87 | <% end %> |
|
89 | <% end %> | |
88 | </div> |
|
90 | </div> | |
89 | <script type="text/javascript">toggle_filter('<%= field %>');</script> |
|
91 | <script type="text/javascript">toggle_filter('<%= field %>');</script> | |
90 | </td> |
|
92 | </td> | |
91 | </tr> |
|
93 | </tr> | |
92 | <% end %> |
|
94 | <% end %> | |
93 | </table> |
|
95 | </table> | |
94 | </td> |
|
96 | </td> | |
95 | <td align="right" valign="top"> |
|
97 | <td align="right" valign="top"> | |
96 | <%= l(:label_filter_add) %>: |
|
98 | <%= l(:label_filter_add) %>: | |
97 | <%= select_tag 'add_filter_select', options_for_select([["",""]] + query.available_filters.sort{|a,b| a[1][:order]<=>b[1][:order]}.collect{|field| [ field[1][:name] || l(("field_"+field[0].to_s.gsub(/\_id$/, "")).to_sym), field[0]] unless query.has_filter?(field[0])}.compact), :onchange => "add_filter();", :class => "select-small" %> |
|
99 | <%= select_tag 'add_filter_select', options_for_select([["",""]] + query.available_filters.sort{|a,b| a[1][:order]<=>b[1][:order]}.collect{|field| [ field[1][:name] || l(("field_"+field[0].to_s.gsub(/\_id$/, "")).to_sym), field[0]] unless query.has_filter?(field[0])}.compact), :onchange => "add_filter();", :class => "select-small" %> | |
98 | </td> |
|
100 | </td> | |
99 | </tr> |
|
101 | </tr> | |
100 | </table> |
|
102 | </table> | |
101 | </fieldset> No newline at end of file |
|
103 | </fieldset> |
General Comments 0
You need to be logged in to leave comments.
Login now