@@ -1,257 +1,257 | |||
|
1 | 1 | # redMine - project management software |
|
2 | 2 | # Copyright (C) 2006-2007 Jean-Philippe Lang |
|
3 | 3 | # |
|
4 | 4 | # This program is free software; you can redistribute it and/or |
|
5 | 5 | # modify it under the terms of the GNU General Public License |
|
6 | 6 | # as published by the Free Software Foundation; either version 2 |
|
7 | 7 | # of the License, or (at your option) any later version. |
|
8 | 8 | # |
|
9 | 9 | # This program is distributed in the hope that it will be useful, |
|
10 | 10 | # but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
11 | 11 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
12 | 12 | # GNU General Public License for more details. |
|
13 | 13 | # |
|
14 | 14 | # You should have received a copy of the GNU General Public License |
|
15 | 15 | # along with this program; if not, write to the Free Software |
|
16 | 16 | # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. |
|
17 | 17 | |
|
18 | 18 | class Query < ActiveRecord::Base |
|
19 | 19 | belongs_to :project |
|
20 | 20 | belongs_to :user |
|
21 | 21 | serialize :filters |
|
22 | 22 | |
|
23 | 23 | attr_protected :project, :user |
|
24 | 24 | attr_accessor :executed_by |
|
25 | 25 | |
|
26 | 26 | validates_presence_of :name, :on => :save |
|
27 | 27 | validates_length_of :name, :maximum => 255 |
|
28 | 28 | |
|
29 | 29 | @@operators = { "=" => :label_equals, |
|
30 | 30 | "!" => :label_not_equals, |
|
31 | 31 | "o" => :label_open_issues, |
|
32 | 32 | "c" => :label_closed_issues, |
|
33 | 33 | "!*" => :label_none, |
|
34 | 34 | "*" => :label_all, |
|
35 | 35 | "<t+" => :label_in_less_than, |
|
36 | 36 | ">t+" => :label_in_more_than, |
|
37 | 37 | "t+" => :label_in, |
|
38 | 38 | "t" => :label_today, |
|
39 | 39 | "w" => :label_this_week, |
|
40 | 40 | ">t-" => :label_less_than_ago, |
|
41 | 41 | "<t-" => :label_more_than_ago, |
|
42 | 42 | "t-" => :label_ago, |
|
43 | 43 | "~" => :label_contains, |
|
44 | 44 | "!~" => :label_not_contains } |
|
45 | 45 | |
|
46 | 46 | cattr_reader :operators |
|
47 | 47 | |
|
48 | 48 | @@operators_by_filter_type = { :list => [ "=", "!" ], |
|
49 | 49 | :list_status => [ "o", "=", "!", "c", "*" ], |
|
50 | 50 | :list_optional => [ "=", "!", "!*", "*" ], |
|
51 | 51 | :list_one_or_more => [ "*", "=" ], |
|
52 | 52 | :date => [ "<t+", ">t+", "t+", "t", "w", ">t-", "<t-", "t-" ], |
|
53 | 53 | :date_past => [ ">t-", "<t-", "t-", "t", "w" ], |
|
54 | 54 | :string => [ "=", "~", "!", "!~" ], |
|
55 | 55 | :text => [ "~", "!~" ] } |
|
56 | 56 | |
|
57 | 57 | cattr_reader :operators_by_filter_type |
|
58 | 58 | |
|
59 | 59 | def initialize(attributes = nil) |
|
60 | 60 | super attributes |
|
61 | 61 | self.filters ||= { 'status_id' => {:operator => "o", :values => [""]} } |
|
62 | 62 | end |
|
63 | 63 | |
|
64 | 64 | def executed_by=(user) |
|
65 | 65 | @executed_by = user |
|
66 | 66 | set_language_if_valid(user.language) if user |
|
67 | 67 | end |
|
68 | 68 | |
|
69 | 69 | def validate |
|
70 | 70 | filters.each_key do |field| |
|
71 | 71 | errors.add label_for(field), :activerecord_error_blank unless |
|
72 | 72 | # filter requires one or more values |
|
73 | 73 | (values_for(field) and !values_for(field).first.empty?) or |
|
74 | 74 | # filter doesn't require any value |
|
75 | 75 | ["o", "c", "!*", "*", "t", "w"].include? operator_for(field) |
|
76 | 76 | end if filters |
|
77 | 77 | end |
|
78 | 78 | |
|
79 | 79 | def editable_by?(user) |
|
80 | 80 | return false unless user |
|
81 | 81 | return true if !is_public && self.user_id == user.id |
|
82 | 82 | is_public && user.allowed_to?(:manage_pulic_queries, project) |
|
83 | 83 | end |
|
84 | 84 | |
|
85 | 85 | def available_filters |
|
86 | 86 | return @available_filters if @available_filters |
|
87 | 87 | @available_filters = { "status_id" => { :type => :list_status, :order => 1, :values => IssueStatus.find(:all, :order => 'position').collect{|s| [s.name, s.id.to_s] } }, |
|
88 | 88 | "tracker_id" => { :type => :list, :order => 2, :values => Tracker.find(:all, :order => 'position').collect{|s| [s.name, s.id.to_s] } }, |
|
89 | 89 | "priority_id" => { :type => :list, :order => 3, :values => Enumeration.find(:all, :conditions => ['opt=?','IPRI']).collect{|s| [s.name, s.id.to_s] } }, |
|
90 | 90 | "subject" => { :type => :text, :order => 8 }, |
|
91 | 91 | "created_on" => { :type => :date_past, :order => 9 }, |
|
92 | 92 | "updated_on" => { :type => :date_past, :order => 10 }, |
|
93 | 93 | "start_date" => { :type => :date, :order => 11 }, |
|
94 | 94 | "due_date" => { :type => :date, :order => 12 } } |
|
95 | 95 | |
|
96 | 96 | user_values = [] |
|
97 | 97 | if project |
|
98 | 98 | user_values += project.users.collect{|s| [s.name, s.id.to_s] } |
|
99 | 99 | elsif executed_by |
|
100 | 100 | user_values << ["<< #{l(:label_me)} >>", "me"] if executed_by |
|
101 | 101 | # members of the user's projects |
|
102 | 102 | user_values += executed_by.projects.collect(&:users).flatten.uniq.sort.collect{|s| [s.name, s.id.to_s] } |
|
103 | 103 | end |
|
104 | 104 | @available_filters["assigned_to_id"] = { :type => :list_optional, :order => 4, :values => user_values } unless user_values.empty? |
|
105 | 105 | @available_filters["author_id"] = { :type => :list, :order => 5, :values => user_values } unless user_values.empty? |
|
106 | 106 | |
|
107 | 107 | if project |
|
108 | 108 | # project specific filters |
|
109 | 109 | @available_filters["category_id"] = { :type => :list_optional, :order => 6, :values => @project.issue_categories.collect{|s| [s.name, s.id.to_s] } } |
|
110 | 110 | @available_filters["fixed_version_id"] = { :type => :list_optional, :order => 7, :values => @project.versions.sort.collect{|s| [s.name, s.id.to_s] } } |
|
111 | 111 | unless @project.active_children.empty? |
|
112 | 112 | @available_filters["subproject_id"] = { :type => :list_one_or_more, :order => 13, :values => @project.active_children.collect{|s| [s.name, s.id.to_s] } } |
|
113 | 113 | end |
|
114 | 114 | @project.all_custom_fields.select(&:is_filter?).each do |field| |
|
115 | 115 | case field.field_format |
|
116 | 116 | when "string", "int" |
|
117 | 117 | options = { :type => :string, :order => 20 } |
|
118 | 118 | when "text" |
|
119 | 119 | options = { :type => :text, :order => 20 } |
|
120 | 120 | when "list" |
|
121 | 121 | options = { :type => :list_optional, :values => field.possible_values, :order => 20} |
|
122 | 122 | when "date" |
|
123 | 123 | options = { :type => :date, :order => 20 } |
|
124 | 124 | when "bool" |
|
125 | 125 | options = { :type => :list, :values => [[l(:general_text_yes), "1"], [l(:general_text_no), "0"]], :order => 20 } |
|
126 | 126 | end |
|
127 | 127 | @available_filters["cf_#{field.id}"] = options.merge({ :name => field.name }) |
|
128 | 128 | end |
|
129 | 129 | # remove category filter if no category defined |
|
130 | 130 | @available_filters.delete "category_id" if @available_filters["category_id"][:values].empty? |
|
131 | 131 | end |
|
132 | 132 | @available_filters |
|
133 | 133 | end |
|
134 | 134 | |
|
135 | 135 | def add_filter(field, operator, values) |
|
136 | 136 | # values must be an array |
|
137 | 137 | return unless values and values.is_a? Array # and !values.first.empty? |
|
138 | 138 | # check if field is defined as an available filter |
|
139 | 139 | if available_filters.has_key? field |
|
140 | 140 | filter_options = available_filters[field] |
|
141 | 141 | # check if operator is allowed for that filter |
|
142 | 142 | #if @@operators_by_filter_type[filter_options[:type]].include? operator |
|
143 | 143 | # allowed_values = values & ([""] + (filter_options[:values] || []).collect {|val| val[1]}) |
|
144 | 144 | # filters[field] = {:operator => operator, :values => allowed_values } if (allowed_values.first and !allowed_values.first.empty?) or ["o", "c", "!*", "*", "t"].include? operator |
|
145 | 145 | #end |
|
146 | 146 | filters[field] = {:operator => operator, :values => values } |
|
147 | 147 | end |
|
148 | 148 | end |
|
149 | 149 | |
|
150 | 150 | def add_short_filter(field, expression) |
|
151 | 151 | return unless expression |
|
152 | 152 | parms = expression.scan(/^(o|c|\!|\*)?(.*)$/).first |
|
153 | 153 | add_filter field, (parms[0] || "="), [parms[1] || ""] |
|
154 | 154 | end |
|
155 | 155 | |
|
156 | 156 | def has_filter?(field) |
|
157 | 157 | filters and filters[field] |
|
158 | 158 | end |
|
159 | 159 | |
|
160 | 160 | def operator_for(field) |
|
161 | 161 | has_filter?(field) ? filters[field][:operator] : nil |
|
162 | 162 | end |
|
163 | 163 | |
|
164 | 164 | def values_for(field) |
|
165 | 165 | has_filter?(field) ? filters[field][:values] : nil |
|
166 | 166 | end |
|
167 | 167 | |
|
168 | 168 | def label_for(field) |
|
169 | 169 | label = @available_filters[field][:name] if @available_filters.has_key?(field) |
|
170 | 170 | label ||= field.gsub(/\_id$/, "") |
|
171 | 171 | end |
|
172 | 172 | |
|
173 | 173 | def statement |
|
174 | 174 | # project/subprojects clause |
|
175 | 175 | clause = '' |
|
176 | 176 | if project && has_filter?("subproject_id") |
|
177 | 177 | subproject_ids = [] |
|
178 | 178 | if operator_for("subproject_id") == "=" |
|
179 | 179 | subproject_ids = values_for("subproject_id").each(&:to_i) |
|
180 | 180 | else |
|
181 | 181 | subproject_ids = project.active_children.collect{|p| p.id} |
|
182 | 182 | end |
|
183 | 183 | clause << "#{Issue.table_name}.project_id IN (%d,%s)" % [project.id, subproject_ids.join(",")] if project |
|
184 | 184 | elsif project |
|
185 | 185 | clause << "#{Issue.table_name}.project_id=%d" % project.id |
|
186 | 186 | else |
|
187 | 187 | clause << Project.visible_by(executed_by) |
|
188 | 188 | end |
|
189 | 189 | |
|
190 | 190 | # filters clauses |
|
191 | 191 | filters_clauses = [] |
|
192 | 192 | filters.each_key do |field| |
|
193 | 193 | next if field == "subproject_id" |
|
194 | 194 | v = values_for(field).clone |
|
195 | 195 | next unless v and !v.empty? |
|
196 | 196 | |
|
197 | 197 | sql = '' |
|
198 | 198 | if field =~ /^cf_(\d+)$/ |
|
199 | 199 | # custom field |
|
200 | 200 | db_table = CustomValue.table_name |
|
201 | 201 | db_field = 'value' |
|
202 | 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 " |
|
203 | 203 | else |
|
204 | 204 | # regular field |
|
205 | 205 | db_table = Issue.table_name |
|
206 | 206 | db_field = field |
|
207 | 207 | sql << '(' |
|
208 | 208 | end |
|
209 | 209 | |
|
210 | 210 | # "me" value subsitution |
|
211 | 211 | if %w(assigned_to_id author_id).include?(field) |
|
212 | 212 | v.push(executed_by ? executed_by.id.to_s : "0") if v.delete("me") |
|
213 | 213 | end |
|
214 | 214 | |
|
215 | 215 | case operator_for field |
|
216 | 216 | when "=" |
|
217 | 217 | sql = sql + "#{db_table}.#{db_field} IN (" + v.collect{|val| "'#{connection.quote_string(val)}'"}.join(",") + ")" |
|
218 | 218 | when "!" |
|
219 | 219 | sql = sql + "#{db_table}.#{db_field} NOT IN (" + v.collect{|val| "'#{connection.quote_string(val)}'"}.join(",") + ")" |
|
220 | 220 | when "!*" |
|
221 | 221 | sql = sql + "#{db_table}.#{db_field} IS NULL" |
|
222 | 222 | when "*" |
|
223 | 223 | sql = sql + "#{db_table}.#{db_field} IS NOT NULL" |
|
224 | 224 | when "o" |
|
225 | 225 | sql = sql + "#{IssueStatus.table_name}.is_closed=#{connection.quoted_false}" if field == "status_id" |
|
226 | 226 | when "c" |
|
227 | 227 | sql = sql + "#{IssueStatus.table_name}.is_closed=#{connection.quoted_true}" if field == "status_id" |
|
228 | 228 | when ">t-" |
|
229 | 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)] |
|
230 | 230 | when "<t-" |
|
231 | 231 | sql = sql + "#{db_table}.#{db_field} <= '%s'" % connection.quoted_date((Date.today - v.first.to_i).to_time) |
|
232 | 232 | when "t-" |
|
233 | 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)] |
|
234 | 234 | when ">t+" |
|
235 | 235 | sql = sql + "#{db_table}.#{db_field} >= '%s'" % connection.quoted_date((Date.today + v.first.to_i).to_time) |
|
236 | 236 | when "<t+" |
|
237 | 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)] |
|
238 | 238 | when "t+" |
|
239 | 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)] |
|
240 | 240 | when "t" |
|
241 | 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)] |
|
242 | 242 | 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)] | |
|
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)] | |
|
244 | 244 | when "~" |
|
245 | 245 | sql = sql + "#{db_table}.#{db_field} LIKE '%#{connection.quote_string(v.first)}%'" |
|
246 | 246 | when "!~" |
|
247 | 247 | sql = sql + "#{db_table}.#{db_field} NOT LIKE '%#{connection.quote_string(v.first)}%'" |
|
248 | 248 | end |
|
249 | 249 | sql << ')' |
|
250 | 250 | filters_clauses << sql |
|
251 | 251 | end if filters and valid? |
|
252 | 252 | |
|
253 | 253 | clause << ' AND ' unless clause.empty? |
|
254 | 254 | clause << filters_clauses.join(' AND ') unless filters_clauses.empty? |
|
255 | 255 | clause |
|
256 | 256 | end |
|
257 | 257 | end |
General Comments 0
You need to be logged in to leave comments.
Login now